Loop through a structured array? and referencing individual elements

4 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 31 de Jul. de 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.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by