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

3 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Stephen23
Stephen23 am 4 Jul. 2018
Bearbeitet: Stephen23 am 4 Jul. 2018
  4 Kommentare
Stephen23
Stephen23 am 8 Jul. 2018
@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.
David Horton
David Horton am 9 Jul. 2018
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Produkte


Version

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by