Loop through a structured array? and referencing individual elements

4 vues (au cours des 30 derniers jours)
Elin
Elin le 31 Juil 2011
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 31 Juil 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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by