is there a way to extract a set of values from a structured array?

I have defined a structured array, meas, which is indexed by number of occurrences of a phenomena. Thus, for example, I have: meas(n).ti = 65.32; meas(n).Rt = 200; meas(n).Vt = 10; etc., etc. I want to define a vector of, for example, the times of occurrences (the "ti" values). The pseudo-code would look like: 'TI = meas(1:end).ti' I obviously could do this in a loop: for n=1:nocc; TI(n) = meas(n).ti; end; (where nocc is the # of occurrences.) Is there a way to do this without using a loop?

 Respuesta aceptada

Stephen23
Stephen23 el 4 de Jul. de 2018
Editada: Stephen23 el 4 de Jul. de 2018

4 comentarios

Thanks, Stephen. This is a great help and now I know a little bit more about structured arrays ....
David Horton
David Horton el 8 de Jul. de 2018
Editada: Stephen23 el 8 de Jul. de 2018
I have a follow-up along the same lines ... On each occurrence, for the n-th object, I define an array as above - with data:
object(n).meas.ti = 23;
object(n).meas.Rt = 75;
object(n).meas.Vt = 7;
etc.
I need to keep a record of each object's measurements so I defined a nested structure (my terminology - I don't know if it's correct) to keep records on each object:
OBJECT(n).meas(m).ti;
OBJECT(n).meas(m).Rt;
OBJECT(n).meas(m).Vt;
etc.
This second structure thus has a set of measures for EACH OCCURRENCE, m. I would like to be able to simply save data from the first structure into the second, i.e.:
OBJECT(n).meas(m).ti = object(n).meas.ti
as they occur but I don't see how to do it. When I try a straightforward approach such as above I get the error, "Subscripted assignment between dissimilar structures," presumably because of the difference in 'dimensionality.' I have tried numerous ways to do this, including making use of comma-separated lists but have been unsuccessful so far. Do you have a suggestion? (I'd really like to work with data structures instead of just defining multi-dimensional arrays of variables - if possible.)
@David Horton: this is exactly why I avoid working with nested structures: there is no short-cut or convenient syntax to accessing data in nested structures, so you will have to use loops. Note that getfield and setfield do work with nested structures (but only for one individual field at a time).
In the message "dissimilar structures" refers to the fields, not the size, it simply means that the fields do not match up.
Personally I use multi-dimensional arrays as much as possible, because it significantly simplifies the code required to access and process the data. My advice would be to use multi-dimensional arrays, as that is the easiest way to write neat and efficient MATLAB code.
Thanks. I actually had coded all of this with multi-D arrays to begin with and it was suggested I switch to structured arrays because they were "easier to work with." I shall revert to my old code.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2015b

Preguntada:

el 4 de Jul. de 2018

Comentada:

el 9 de Jul. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by