How to collect data into a vector from this data structure?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mr M.
am 29 Jan. 2018
Kommentiert: Stephen23
am 29 Jan. 2018
I have A.B(i,j).C(k).D(n).E.F, and i,j,k are fixed, but n runs from 1 to N. How to collect data into a vector? I know that it is possible to write a for cycle to do this, but are there any simpler solution in one line to do this? A.B(i,j).C(k).D(:).E.F not works for example.
0 Kommentare
Akzeptierte Antwort
Cam Salzberger
am 29 Jan. 2018
You can make use of the ability to output a comma-separated list of values from a single-level structure array. In other words, if you make a structure array like so:
eg = struct('field', {1 2 3})
Then you can do this:
>> eg.field
ans =
1
ans =
2
ans =
3
Which means you can put that together into a vector with square brackets:
>> [eg.field]
ans =
1 2 3
Now, since you have a nested structure, you just have to take the intermediate step of making a structure array first, then make a numeric array from that structure array.
Example structure:
for n = 1:10
A.B(1,1).C(1).D(n).E.F = n;
end
Extraction:
S = [A.B(1,1).C(1).D(:).E]; % Structure array
V = [S(:).F]; % Numeric array
You can change out the fixed indices as you need.
-Cam
1 Kommentar
Stephen23
am 29 Jan. 2018
Read about this in the MATLAB documentation:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!