How to print multiple Values using sprintf In matlab

28 visualizaciones (últimos 30 días)
Stephen john
Stephen john el 31 de Ag. de 2022
Comentada: Stephen john el 31 de Ag. de 2022
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 '

Respuesta aceptada

Stephen23
Stephen23 el 31 de Ag. de 2022
Editada: Stephen23 el 31 de Ag. de 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
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 comentarios
Stephen23
Stephen23 el 31 de Ag. de 2022
Editada: Stephen23 el 31 de Ag. de 2022
"Can you please modified it"
You can use indexing to access cell array content:
S = load('newone.mat');
T = S.CombineOutput.PRFStructure
T = 1×4 table
StagLevels StagPRFValue StaggMinimumValue StaggMaximumValue __________ ____________ _________________ _________________ 9 {9×1 double} 2838 2784
% vvv very basic cell array indexing
join(string(T.StagPRFValue{1}),' ')
ans = "2784 2787 2789 2793 2801 2802 2823 2824 2838"

Iniciar sesión para comentar.

Más respuestas (1)

Karim
Karim el 31 de Ag. de 2022
Editada: Karim el 31 de Ag. de 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
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 comentarios
Stephen john
Stephen john el 31 de Ag. de 2022
@Karim But it need to be uniform sometime i get DS Levels: 3 sometimes its DS Levels: 4
and DSValue also change with Respect to Levels.
Karim
Karim el 31 de Ag. de 2022
I updated the answer so that the amount of %d's is determined automatically

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by