Loop through a structured array? and referencing individual elements

4 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Walter Roberson
Walter Roberson il 31 Lug 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.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by