Loop through a structured array? and referencing individual elements
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponse acceptée
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.
0 commentaires
Plus de réponses (0)
Voir également
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!