Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
using getfield() with a stuct array

Subject: using getfield() with a stuct array

From: Bruce Elliott

Date: 18 Jan, 2013 18:16:07

Message: 1 of 4

Is there any way to use getfield() on an array of structure and to get a same-sized array of field values?

In other words, I can do this:

x = [myArr.F];

to get an array with the values in the field F in my stuct array, myArr.

This, however, does not work:

x = getfield(myArr,'F');

(Putting [] around anything on the right-hand-side does not help.)

There's probably a good reason for this, but the documentation suggests that x = getfield(S,'F') is equivalent to x = S.F, so I would have hoped that I could do this.

Subject: using getfield() with a stuct array

From: James Tursa

Date: 18 Jan, 2013 21:02:08

Message: 2 of 4

"Bruce Elliott" <bruce.elliott@jhuapl.nospam.edu> wrote in message <kdc3h7$d6b$1@newscl01ah.mathworks.com>...
> Is there any way to use getfield() on an array of structure and to get a same-sized array of field values?
>
> In other words, I can do this:
>
> x = [myArr.F];
>
> to get an array with the values in the field F in my stuct array, myArr.
>
> This, however, does not work:
>
> x = getfield(myArr,'F');
>
> (Putting [] around anything on the right-hand-side does not help.)
>
> There's probably a good reason for this, but the documentation suggests that x = getfield(S,'F') is equivalent to x = S.F, so I would have hoped that I could do this.

Are you asking this question because you want to dynamically pick the fieldname at runtime? E.g., myArr.(fieldnamevariable)

James Tursa

Subject: using getfield() with a stuct array

From: Bruce Elliott

Date: 18 Jan, 2013 21:12:08

Message: 3 of 4

"James Tursa" wrote in message <kdcd8g$meg$1@newscl01ah.mathworks.com>...
>
> Are you asking this question because you want to dynamically pick the fieldname at runtime? E.g., myArr.(fieldnamevariable)

Yes, exactly. Ultimately, I want to be able to select elements of a structure array for which the fields have certain values, e.g. myStruct([myStruct.X]>10). I can do that fine at the command line, but I'm creating some tools for other users who are not as familiar with structures, so I want to give them a user-friendly interface that will work with whatever filtering criteria they want to use.

I suppose I could do this with a big switch block with separate (otherwise identical) cases for each field, but that seems kind of cumbersome. Instead, I'm looping through the structure array and testing the field values one at a time. That always feels wrong in Matlab.

Subject: using getfield() with a stuct array

From: Steven_Lord

Date: 18 Jan, 2013 22:14:49

Message: 4 of 4



"Bruce Elliott" <bruce.elliott@jhuapl.nospam.edu> wrote in message
news:kdc3h7$d6b$1@newscl01ah.mathworks.com...
> Is there any way to use getfield() on an array of structure and to get a
> same-sized array of field values?
>
> In other words, I can do this:
>
> x = [myArr.F];
>
> to get an array with the values in the field F in my stuct array, myArr.
>
> This, however, does not work:
>
> x = getfield(myArr,'F');
>
> (Putting [] around anything on the right-hand-side does not help.)
>
> There's probably a good reason for this, but the documentation suggests
> that x = getfield(S,'F') is equivalent to x = S.F, so I would have hoped
> that I could do this.

For _scalar_ struct arrays S, yes those are equivalent.

http://www.mathworks.com/help/matlab/ref/getfield.html

"value = getfield(struct, 'field'), where struct is a 1-by-1 structure,
returns the contents of the specified field, equivalent to value =
struct.field."
and
"If structure struct or any of the fields is a nonscalar structure, and you
do not specify an Indx, the getfield function returns the values associated
with the first index."

Try:

thefieldname = 'F';
x = [myArr.(thefieldname)]

You can use this like:

F = struct('x', {2, 5, 17})
z = F(mod([F.('x')], 2) == 1)

This will extract the elements of F whose x fields contain odd values. From
the way I constructed F you would expect z to contain two elements, and it
does.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us