how to index a cell array
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
li yan
am 23 Mai 2018
Kommentiert: li yan
am 23 Mai 2018
Hi, I encountered a Matlab problem again. I want to access all struct field elements, which is an element of a cell array, at one time, not using loop. Such as:
s{1} = struct('ID',1,'other field',...);
s{2} = struct('ID',2,'other field',...);
I want to access all ID values in the cell s, I write the code like this:
IDs = [s{:}.ID];
This does not work, how to access all ID values not using for loop, please?
2 Kommentare
Stephen23
am 23 Mai 2018
If you used just one non-scalar structure (rather than lots of scalar structure in a cell array) then the solution would be very simple:
S(1) = struct('ID',1,'other field',...);
S(2) = struct('ID',2,'other field',...);
[S.ID]
Simpler, more efficient code through better data design. Why make your code more complex than this?
Akzeptierte Antwort
Ameer Hamza
am 23 Mai 2018
You can do this in Two steps
temp = [s{:}];
allIDValues = [temp.ID];
2 Kommentare
Stephen23
am 23 Mai 2018
Note that in Ameer Hamza's answer the first line of code converts your cell array of scalar structures into one non-scalar structure. If you had stored your data in a non-scalar structure in the first place, then you would not need this extra step.
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!