How to print multiple Values using sprintf In matlab
Show older comments
Hello Everyone, I hope you are doing well.
I have the following dataset which consists of following values . I am trying to print the values using sprintf. But extra values are printing
The sprintf output should be like
'DS Levels: 2
DS Value: [333 343]
DS Length: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 23]
Maximum Value of DS:343'
Minimum Value of DS:333'
but i am getting the ouput like the following
'DS Levels: 2
DS Value: [333 343 1]
DS Length: [2 3 4 ]
Maximum Value of DS:5
Minimum Value of DS:6
DS Levels: 7
DS Value: [8 9 10]
DS Length: [11 12 13 ]
Maximum Value of DS:14
Minimum Value of DS:15
DS Levels: 16
DS Value: [17 19 23]
DS Length: [343 333 '
z=1;
pred1 = sprintf('DS Levels: %d\n DS Value: [%d %d %d]\n DS Length: [%d %d %d ]\n Maximum Value of DS:%d\n Minimum Value of DS:%d\n ',CombineOutput(z).PRFStructure.Levels,unique(CombineOutput(z).PRFStructure.DSPRFValue),unique(CombineOutput(z).PRFStructure.DSlength),CombineOutput(z).PRFStructure.DSmaximum,CombineOutput(z).PRFStructure.DSminimum);
pred1
%% Its print the values the like following
% % % % 'DS Levels: 2
% DS Value: [333 343 1]
% DS Length: [2 3 4 ]
% Maximum Value of DS:5
% Minimum Value of DS:6
% DS Levels: 7
% DS Value: [8 9 10]
% DS Length: [11 12 13 ]
% Maximum Value of DS:14
% Minimum Value of DS:15
% DS Levels: 16
% DS Value: [17 19 23]
% DS Length: [343 333 '
Accepted Answer
More Answers (1)
This is due to the amount %d you have used. Only use 2 of them for DS Value and 19 for DS Length
update added a 'print string' which updates the amount of %d's automatically based on the struct
load(websave('myFile', "https://nl.mathworks.com/matlabcentral/answers/uploaded_files/1112565/matlab.mat"))
z = 1;
% create the print string
PrintStr = join(["DS Levels: ",repmat("%d ",1,numel(CombineOutput(z).PRFStructure.Levels)),"\n"...
"DS Value: ", repmat("%d ",1,numel(unique(CombineOutput(z).PRFStructure.DSPRFValue))),"\n"...
"DS Length: ",repmat("%d ",1,numel(unique(CombineOutput(z).PRFStructure.DSlength))),"\n"...
"Maximum Value of DS: ",repmat("%d ",1,numel(CombineOutput(z).PRFStructure.DSmaximum)),"\n"...
"Minimum Value of DS: ",repmat("%d ",1,numel(CombineOutput(z).PRFStructure.DSminimum)),"\n"]);
% print the text
pred1 = sprintf(PrintStr,CombineOutput(z).PRFStructure.Levels,unique(CombineOutput(z).PRFStructure.DSPRFValue),unique(CombineOutput(z).PRFStructure.DSlength),CombineOutput(z).PRFStructure.DSmaximum,CombineOutput(z).PRFStructure.DSminimum);
pred1
2 Comments
Stephen john
on 31 Aug 2022
Karim
on 31 Aug 2022
I updated the answer so that the amount of %d's is determined automatically
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!