Loop through a structured array? and referencing individual elements

1 view (last 30 days)
is there a way to loop through a structured array?
A.A=2 A.B=9 A.C=5 A.D=6
is there a way to loop through A to create a vector [2 9 5 6]
and is there an alternative way to reference the value 2 other Than using A.A?

Accepted Answer

Walter Roberson
Walter Roberson on 31 Jul 2011
Converting to row vector:
cell2mat(struct2cell(A)).'
Referencing the 2: structured arrays do not really have a defined order of the fields, but in practice the field names are ordered according to which one was assigned first. If you knew the relative order of the field, you could struct2cell() and access the corresponding cell entry. You could also use fieldnames() to extract the field names as a whole, choose the one at the proper relative offset, and use dynamic fieldnames.
There is no defined MATLAB syntax to access a field according to its relative order, but the above two methods can work, or you could use a small MEX routine.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!