How to print multiple Values using sprintf In matlab
28 views (last 30 days)
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 '
0 Comments
Accepted Answer
Stephen23
on 31 Aug 2022
Edited: Stephen23
on 31 Aug 2022
Rather than hard-coding a fixed number of values to print, I recommend writing more robust code using STRING &JOIN or COMPOSE or SPRINTF to first create text of any number of values, then provide those to SPRINTF. For example:
S = load('matlab.mat');
T = S.CombineOutput.PRFStructure
Z = sprintf('DS Levels: %d\nDS Value: [%s]\nDS Length: [%s]\nMaximum Value of DS:%d\nMinimum Value of DS:%d',...
T.Levels, join(string(T.DSPRFValue),' '), join(string(T.DSlength),' '), T.DSmaximum, T.DSminimum)
If the text is ultimately going to be displayed in the command window or printed to a file, then skip SPRINTF and just go directly to FPRINTF:
fprintf('DS Levels: %d\nDS Value: [%d', T.Levels, T.DSPRFValue(1));
fprintf(' %d', T.DSPRFValue(2:end));
fprintf(']\nDS Length: [%d',T.DSlength(1));
fprintf(' %d', T.DSlength(2:end));
fprintf(']\nMaximum Value of DS:%d\nMinimum Value of DS:%d', T.DSmaximum, T.DSminimum)
That would probably be the most efficient approach.
More Answers (1)
Karim
on 31 Aug 2022
Edited: Karim
on 31 Aug 2022
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
See Also
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!