Plotting data from struct array
147 views (last 30 days)
Show older comments
I am having trouble plotting data that is stored in a struct array. The data is stored in the struct array AB. Let's say it is N-dimensional. So, AB(1), AB(2),.., AB(N). Each has various fields and sub-fields. I tried:
plot(AB.fv.tot, AB.sv.tot)
This yields "expected one output form a curly brace or dot indexing expression, but there were N results". I think I understand that. Invoking the same command as:
plot(AB(:).fv.tot, AB(:).sv.tot)
doesn't achieve anything different, which also makes sense. I saw some suggestions to do the following:
plot([AB.fv.tot], [AB.sv.tot])
This too yields the same result. I am really not sure how to call plot with the plot vectors being contained in fields/subfields of a struct array.
2 Comments
the cyclist
on 20 Jan 2023
Can you upload the AB variable? You can use the paper clip icon in the INSERT section of the toolbar.
Stephen23
on 20 Jan 2023
"Let's say it is N-dimensional. So, AB(1), AB(2),.., AB(N)"
What you describe is N elements of a structure array, not N dimensions.
Yes, structures can be multi-dimensional. Here is a 4D structure:
S = struct('X',cell(5,4,3,2))
Accepted Answer
Stephen23
on 20 Jan 2023
Edited: Stephen23
on 20 Jan 2023
"I am really not sure how to call plot with the plot vectors being contained in fields/subfields of a struct array."
The answer depends mostly on the sizes of all of the structures and the sizes of their fields.
But you did not tell us this.
"This too yields the same result. "
Of course it does: dot indexing into a non-scalar structure returns a comma-separated list. You get the error because it is not possible (or defined) to perform further dot indexing on a comma-separated list:
Before knowing how to "flatten" a set of nested structures you need to know the sizes of all of them. Only then can you judiciously apply a few comma-separated lists to "flatten" that data:
First you need to know exactly the sizes of all of your structures and fields.
Because you did not tell us this important information I am going to assume that all sub-structures are scalar and all their fields are scalar numeric... which may or may not reflect your actual data. But clearly if you want a working solution for your data, you would need to either a) provide an accurate description of your data, or b) upload your data by clicking the paperclip button.
Sfv = [AB.fv];
Ssv = [AB.sv];
plot([Sfv.tot], [Ssv.tot])
You will not get much further if you do not know the exact sizes of all of the structures and their field data.
3 Comments
More Answers (1)
Sulaymon Eshkabilov
on 20 Jan 2023
Here is one simple example:
t1 = linspace(-pi, pi, 200);
t2 = linspace(-pi, pi, 500);
f1 = 2*sin(t1);
f2 = 3*cos(t1);
f3 = f1./f2;
f4 = f2./f1;
F5 = 2.5* sin(2*t2);
F6 = 3*cos(3*t2);
H(1).Time=t1;
H(2).Time=t2;
H(1).F=f1;
H(2).F=f2;
H(3).F = f3;
H(4).F = f4;
H(1).FUN = F5;
H(2).FUN = F6;
figure
plot(H(1).Time, H(1).F, 'r--', H(1).Time, H(2).F, 'k', 'linewidth', 2)
figure
plot(H(2).Time, H(1).FUN, 'r--', H(2).Time, H(2).FUN, 'k')
0 Comments
See Also
Categories
Find more on Data Type Identification 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!