How to print multiple Values using sprintf In matlab
46 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Stephen23
2022-8-31
编辑:Stephen23
2022-8-31
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.
更多回答(1 个)
Karim
2022-8-31
编辑:Karim
2022-8-31
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!