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

3 views (last 30 days)
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?

Accepted Answer

Stephen23
Stephen23 on 4 Jul 2018
Edited: Stephen23 on 4 Jul 2018
  4 Comments
Stephen23
Stephen23 on 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 on 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!