How to print multiple Values using sprintf In matlab

18 ビュー (過去 30 日間)
Stephen john
Stephen john 2022 年 8 月 31 日
コメント済み: Stephen john 2022 年 8 月 31 日
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 '

採用された回答

Stephen23
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
T = 1×5 table
DSPRFValue DSlength Levels DSmaximum DSminimum ___________ ___________ ______ _________ _________ 1×85 double 1×85 double 2 343 333
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)
Z =
'DS Levels: 2 DS Value: [333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333 343 333] DS Length: [17 16 16 17 17 16 16 17 17 16 16 17 11 12 17 16 14 17 6 14 16 16 17 4 2 3 6 12 17 8 4 3 4 6 17 13 8 1 1 10 3 2 17 14 7 14 19 14 6 15 16 17 17 6 3 9 4 2 17 7 3 5 4 1 17 10 8 5 17 14 10 12 17 16 16 17 17 10 23 16 16 17 17 16 14] Maximum Value of DS:343 Minimum Value of DS:333'
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.
  3 件のコメント
Stephen john
Stephen john 2022 年 8 月 31 日
@Stephen23 Thanks

サインインしてコメントする。

その他の回答 (1 件)

Karim
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
pred1 =
"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 "
  2 件のコメント
Karim
Karim 2022 年 8 月 31 日
I updated the answer so that the amount of %d's is determined automatically

サインインしてコメントする。

カテゴリ

Find more on Resizing and Reshaping Matrices 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!

Translated by