How to print value from Structure in MATLAB

Hello Everyone, I hope you are doing well, I have the following Structure which include Prediction and ValueStructure
I want to print the Value from ValueStructure
for Class 1 i have write the following Code.
pred2 = sprintf('Class1 Levels: %d\n Maximum Value of Class1:%d\n Minimum Value of Class1:%d\n',CombineOutput(1).PRFStructure.StagLevels,CombineOutput(1).PRFStructure.StaggMinimumValue,CombineOutput(1).PRFStructure.StaggMaximumValue);
But i am unable to print StagPRFValue. How can i print it.
For Class 2
pred1 = sprintf('Class 2 Levels: %d\n Maximum Value of Class 2:%d\n Minimum Value of Class 2:%d\n ',CombineOutput(2).PRFStructure.Levels,CombineOutput(2).PRFStructure.DSmaximum,CombineOutput(2).PRFStructure.DSminimum);
I want to print unique values of DSPRFValue and DS Length
How can i do that in Matlab

Answers (2)

Jonas
Jonas on 4 Jul 2022
Edited: Jonas on 4 Jul 2022
you can access the 4 doubles in the cell StagPRFValue by e.g.
CombineOutput(1).ValueStructure.StagPRFValue{1}
the other two values can be accessed with
CombineOutput(2).ValueStructure.DSPRFValue
and
CombineOutput(2).ValueStructure.DSlength
if you want the unique values of those vectors, use unique(CombineOutput(2).ValueStructure.DSlength)

6 Comments

@Jonas i am using for loop but it does not be written the second cause i want it to be written in array.
@Jonas First one is also not working
the provided m file does not have the fieldnames you wrote in your question, e,g, neither of of your two structs of CombineOutput has a field PRFStructure
load('matlab (1).mat','CombineOutput')
CombineOutput(1)
ans = struct with fields:
Prediction: "Class1" ValueStructure: [1×4 table]
CombineOutput(1).ValueStructure
ans = 1×4 table
StagLevels StagPRFValue StaggMinimumValue StaggMaximumValue __________ ____________ _________________ _________________ 4 {4×1 double} 6600 5400
CombineOutput(2)
ans = struct with fields:
Prediction: "Class2" ValueStructure: [1×5 table]
CombineOutput(2).ValueStructure
ans = 1×5 table
DSPRFValue DSlength Levels DSmaximum DSminimum ___________ ___________ ______ _________ _________ 1×15 double 1×15 double 4 5600 4400
edit: to show that the line DOES work
CombineOutput(1).ValueStructure.StagPRFValue{1}
ans = 4×1
5400 5800 6200 6600
@Jonas But the solution you share does not work.
This one
CombineOutput(1).ValueStructure.StagPRFValue{1}
why does it not work? i edited to previous comment to show, that the syntax is right.
Do you get an error, or did you want another output? please be more specific
@Jonas i am printing this value in the Textbox in app designer, basically it is not working in that

Sign in to comment.

Assuming your sprintf worked to put the right string into pred, you can send pred to the text label component on the GUI like this
app.PredEditField.Value = pred; % Send pred string to the label on the GUI.
If you have any more questions, then attach your .mlapp code after you read this:

10 Comments

@Image Analyst it does not work. StagPRFValue for classone does not show in Textbox of .mlapp
So I guess you're saying that your pred is not right because the sprintf() is not properly constructed? Did you see my comment about attaching your .mlapp file?
@Image Analyst here is my .mlapp code in which i am facing problem
The dataset attached above which is matlab file which includes Output name CombineOutput
i want to print StagPRFValue in pred 2.
I want to print unique values of DSPRFValue and DS Length in pred1
when i used the above solution it does not work.
for z = 1:3
pred = sprintf('Cluster %1.0f Predicted PRI: %s\n',z,CombineOutput(z).Prediction);
app.TextArea.Value{z} = pred;
switch CombineOutput(z).Prediction
case "Class 1"
pred1 = sprintf('Class 2 Levels: %d\n Maximum Value of Class 2:%d\n Minimum Value of Class 2:%d\n ',CombineOutput(z).ValueStructure.Levels,CombineOutput(z).ValueStructure.DSmaximum,CombineOutput(z).ValueStructure.DSminimum);
app.TextArea_2.Value = pred1;
case "Class 2"
pred2 = sprintf('Class1 Levels: %d\n Maximum Value of Class1:%d\n Minimum Value of Class1:%d\n',CombineOutput(z).ValueStructure.StagLevels,CombineOutput(z).ValueStructure.StaggMinimumValue,CombineOutput(z).ValueStructure.StaggMaximumValue);
app.TextArea_3.Value = pred2;
end
end
Ok, so you won't attach the .mlapp file with the paperclip icon. We'll see what we can do without it.
Try this:
%s = load('StephenJohn.mat')
%CombineOutput = s.CombineOutput
for z = 1: numel(CombineOutput)
thisClass = char(CombineOutput(z).Prediction);
predictedPRI = sprintf('Cluster %1.0f Predicted PRI: "%s".\n',z, thisClass);
fprintf('For z = %d, %s\n', z, predictedPRI)
app.TextArea.Value = predictedPRI;
if contains(thisClass, '1')
predictedPRI = sprintf('Class1 Levels: %d\n Maximum Value of Class1:%d\n Minimum Value of Class1:%d\n', ...
CombineOutput(z).ValueStructure.StagLevels, ...
CombineOutput(z).ValueStructure.StaggMinimumValue, ...
CombineOutput(z).ValueStructure.StaggMaximumValue);
app.TextArea_3.Value = predictedPRI;
elseif contains(thisClass, '2')
predictedPRI = sprintf('Class 2 Levels: %d\n Maximum Value of Class 2:%d\n Minimum Value of Class 2:%d\n ', ...
CombineOutput(z).ValueStructure.Levels, ...
CombineOutput(z).ValueStructure.DSmaximum, ...
CombineOutput(z).ValueStructure.DSminimum);
app.TextArea_2.Value = predictedPRI;
end
end
@Image Analyst this is the same code i have used . You have not print StagPRF Value in Class 1 and print unique values of DSPRFValue and DS Length in Class 2.
Have you check the mat file i have attached it consists of these values i want to print these two variable value StagPRF Value in Class 1 and print unique values of DSPRFValue and DS Length in Class 2.
Try this:
s = load('StephenJohn.mat')
s = struct with fields:
CombineOutput: [1×2 struct]
CombineOutput = s.CombineOutput
CombineOutput = 1×2 struct array with fields:
Prediction ValueStructure
for z = 1: numel(CombineOutput)
thisClass = char(CombineOutput(z).Prediction);
predictedPRI = sprintf('Cluster %1.0f Predicted PRI: "%s".\n',z, thisClass);
fprintf('For z = %d, %s\n', z, predictedPRI)
app.TextArea.Value = predictedPRI;
% Get the structure
str = CombineOutput(z).ValueStructure;
% Prepare the string.
if contains(thisClass, '1')
predictedPRI = sprintf('Class1 Levels: %d\n Maximum Value of Class1:%d\n Minimum Value of Class1:%d\n', ...
str.StagLevels, ...
str.StaggMinimumValue, ...
str.StaggMaximumValue)
app.TextArea_3.Value = predictedPRI;
elseif contains(thisClass, '2')
predictedPRI = sprintf('Class 2 Levels: %d\n Maximum Value of Class 2:%d\n Minimum Value of Class 2:%d\n ', ...
str.Levels, ...
str.DSmaximum, ...
str.DSminimum)
app.TextArea_2.Value = predictedPRI;
end
end
For z = 1, Cluster 1 Predicted PRI: "Class1".
predictedPRI =
'Class1 Levels: 4 Maximum Value of Class1:6600 Minimum Value of Class1:5400 '
For z = 2, Cluster 2 Predicted PRI: "Class2".
predictedPRI =
'Class 2 Levels: 4 Maximum Value of Class 2:5600 Minimum Value of Class 2:4400 '
@Image Analyst You have not get my point, The ValueStructure for class 1 has one more variable which is cell name as StagPRFValue ,You are not printing that one, you are only print the remaining three variables.
and for Class 2 ValueStructure consists of two more variable which is DSPRFValue and and DSlength
I'm sure you've figured it out by now. You simply add the variable to the sprintf() statement:
s = load('StephenJohn.mat');
CombineOutput = s.CombineOutput;
for z = 1: numel(CombineOutput)
thisClass = char(CombineOutput(z).Prediction);
predictedPRI = sprintf('Cluster %1.0f Predicted PRI: "%s".\n',z, thisClass);
fprintf('For z = %d, %s\n', z, predictedPRI)
app.TextArea.Value = predictedPRI;
% Get the table from the structure. "ValueStructure" is a field of hte structure and it is a table-type variable.
tbl = CombineOutput(z).ValueStructure;
% Prepare the string.
if contains(thisClass, '1')
StagPRFValues = tbl.StagPRFValue{1};
predictedPRI = sprintf('Class1 Levels: %d\n StagePRFValues: [%d, %d, %d, %d]\n Maximum Value of Class1:%d\n Minimum Value of Class1:%d\n', ...
tbl.StagLevels, ...
StagPRFValues, ...
tbl.StaggMinimumValue, ...
tbl.StaggMaximumValue)
app.TextArea_3.Value = predictedPRI;
elseif contains(thisClass, '2')
predictedPRI = sprintf('Class 2 Levels: %d\n Maximum Value of Class 2:%d\n Minimum Value of Class 2:%d\n ', ...
tbl.Levels, ...
tbl.DSmaximum, ...
tbl.DSminimum)
app.TextArea_2.Value = predictedPRI;
end
end
For z = 1, Cluster 1 Predicted PRI: "Class1".
predictedPRI =
'Class1 Levels: 4 StagePRFValues: [5400, 5800, 6200, 6600] Maximum Value of Class1:6600 Minimum Value of Class1:5400 '
For z = 2, Cluster 2 Predicted PRI: "Class2".
predictedPRI =
'Class 2 Levels: 4 Maximum Value of Class 2:5600 Minimum Value of Class 2:4400 '
@Image Analyst and for Class 2 ValueStructure consists of two more variable which is DSPRFValue and and DSlength which should be printed you have not printed that.
in this dataset StagPRFValues have four values but they should not always four they should be changed How can i print that
I think you could have figured it out yourself knowing how sprintf() works, but anyway, here is what I got to handle a variable number of elements in the StagPRFValues vector.
s = load('StephenJohn.mat');
CombineOutput = s.CombineOutput;
for z = 1: numel(CombineOutput)
thisClass = char(CombineOutput(z).Prediction);
predictedPRI = sprintf('Cluster %1.0f Predicted PRI: "%s".\n',z, thisClass);
fprintf('For z = %d, %s\n', z, predictedPRI)
app.TextArea.Value = predictedPRI;
% Get the table from the structure. "ValueStructure" is a field of hte structure and it is a table-type variable.
tbl = CombineOutput(z).ValueStructure;
% Prepare the string.
if contains(thisClass, '1')
predictedPRI = sprintf('Class1 Levels: %d\n\n StagePRFValues: [', tbl.StagLevels);
StagPRFValues = tbl.StagPRFValue{1};
numValues = numel(StagPRFValues);
for k = 1 : numValues
predictedPRI = sprintf('%s%d ', predictedPRI, StagPRFValues(k));
end
predictedPRI = sprintf('%s]\n Maximum Value of Class1:%d\n Minimum Value of Class1:%d\n', ...
predictedPRI, ...
tbl.StaggMinimumValue, ...
tbl.StaggMaximumValue)
app.TextArea_3.Value = predictedPRI;
elseif contains(thisClass, '2')
predictedPRI = sprintf('Class 2 Levels: %d\n Maximum Value of Class 2:%d\n Minimum Value of Class 2:%d\n ', ...
tbl.Levels, ...
tbl.DSmaximum, ...
tbl.DSminimum)
app.TextArea_2.Value = predictedPRI;
end
end
I think you should be able to add any additional variables you want to print. If not, see sprintf. If you really can't figure it out based on how I wrote out the other variables, then write back. Otherwise, can you click "Accept this answer."?

Sign in to comment.

Categories

Products

Release

R2022a

Asked:

on 4 Jul 2022

Edited:

on 20 Jul 2022

Community Treasure Hunt

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

Start Hunting!