Plotting data from struct array

163 Ansichten (letzte 30 Tage)
AR
AR am 20 Jan. 2023
Kommentiert: AR am 20 Jan. 2023
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 Kommentare
the cyclist
the cyclist am 20 Jan. 2023
Can you upload the AB variable? You can use the paper clip icon in the INSERT section of the toolbar.
Stephen23
Stephen23 am 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))
S = 5×4×3×2 struct array with fields:
X

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 20 Jan. 2023
Bearbeitet: Stephen23 am 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 Kommentare
AR
AR am 20 Jan. 2023
I would upload, but it's taking forever and not completing. Even after I truncated the data to just a 100 values. Perhaps there's something that's blocking the upload on my end.
Regardless, Stephen23's answer makes sense (dot indexing upon a comma separated list). I created some simplified test structures and tried out this answer - it seems to work.
AR
AR am 20 Jan. 2023
Yes, I made the neccessary changes as indicated in Stephen23's answer to the main code and it does work as intended. Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 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')

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by