MATLAB: How to plot a 1 x 1 struct with 175 fields in Matlab

lvmMATLABMATLAB and Simulink Student Suiteplotstruct

I converted my .lvm file into matlab using the function by M. A. Hopcroft ( LVM File Import ).
Now, my workspace has ans as a 1 x 1 struct. The 1 x 1 struct has 175 fields, with 5 extra fields as text*. Thus a total of 180 fields.
I need help plotting my data. In my 1 x 1 struct, I have Segment1 to Segment175. In each, there is a another subfolder called data, in which there are 10000 rows and 2 columns of values. How do I plot ans.Segment1.data, ans.Segment2.data, ans.Segment3.data, … ,ans.Segment175.data into ONE graph?
*Please let me know if the 5 text fields for each segment data confuses matlab in some kind of way.

Best Answer

Unfortunately putting numbers into fieldnames like that makes it all rather tricky, because putting (meta)-data into the fieldnames makes them awkward to handle. The data should have been stored in a non-scalar structure, because indexing is much simpler and more efficient.
As Adam Danz wrote, using ans is a bad idea. Better call your structure s or something more explanatory of its contents. Then you could use dynamic fieldnames like this:
s.Segment1.data = [0,0.00034;0.0001,0.000176;0.0002,0.000176;0.0003,0.000176;0.0004,0.00034;0.9995,0.000505];
s.Segment2.data = [1,0.000176;1.0001,0.000176;1.0002,0.000176;1.0003,0.000669;1.0004,0.00034;1.0005,0.00034];
... all the other fields
s.otherfield = 'blah';
for k = 1:175
fnm = sprintf('Segment%d',k);
plot(s.(fnm).data(:,1),s.(fnm).data(:,2))
hold on
end
Or a a bit neater and clearer would be to use a temporary variable:
for k = 1:175
fnm = sprintf('Segment%d',k);
tmp = s.(fnm).data;
plot(tmp(:,1),tmp(:,2))
hold on
end