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:
getfield

Subject: getfield

From: Bruno Luong

Date: 26 May, 2012 10:21:31

Message: 1 of 3

According to the documentation of getfield

[
value = getfield(struct,{sIndx1,...,sIndxM},'field',{fIndx1,...,fIndxN}) returns the contents of the specified field, equivalent to value = struct(sIndx1,...,sIndxM).field(fIndx1,...,fIndxN). The getfield function supports multiple sets of field and fIndx inputs, and all Indx inputs are optional. 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. If you specify a single colon operator for an fIndx input, enclose it in single quotation marks: ':'.
]

Can someone explains why I can't not get the field of an array:

>> s = struct('f',{1 2})

s =

1x2 struct array with fields:
    f

>> s(1)

ans =

    f: 1

>> s(2)

ans =

    f: 2

>> s(1,1:2).f

ans =

     1


ans =

     2
 
>> values = getfield(s,{1,[1 2]}, 'f')

values =

     1

>> [v1 v2] = getfield(s,{1,[1 2]}, 'f')
Error using getfield
Too many output arguments.
 
% MATLAB 2012A

% Bruno

Subject: getfield

From: Star Strider

Date: 27 May, 2012 14:05:07

Message: 2 of 3

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jpqarb$7jb$1@newscl01ah.mathworks.com>...
> According to the documentation of getfield
>
> [
> value = getfield(struct,{sIndx1,...,sIndxM},'field',{fIndx1,...,fIndxN}) returns the contents of the specified field, equivalent to value = struct(sIndx1,...,sIndxM).field(fIndx1,...,fIndxN). The getfield function supports multiple sets of field and fIndx inputs, and all Indx inputs are optional. 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. If you specify a single colon operator for an fIndx input, enclose it in single quotation marks: ':'.
> ]
>
> Can someone explains why I can't not get the field of an array:
>
> >> s = struct('f',{1 2})

. . .
 
> >> [v1 v2] = getfield(s,{1,[1 2]}, 'f')
> Error using getfield
> Too many output arguments.
>
> % MATLAB 2012A
>
> % Bruno

-----------------------------------------------------------------------

If YOU can’t get MATLAB to do something, you’ve probably discovered a significant bug in ‘getfield’, its documentation, or both. This is a 3-day holiday weekemd in the U.S. (26-28 May), so to be sure your post doesn’t get lost, I suggest you submit it to TMW Tech Support, then when you’ve resolved the problem, post the answer to the Newsgroup.

Subject: getfield

From: Jason

Date: 26 Dec, 2012 02:30:09

Message: 3 of 3

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jpqarb$7jb$1@newscl01ah.mathworks.com>...
> According to the documentation of getfield
>
> [
> value = getfield(struct,{sIndx1,...,sIndxM},'field',{fIndx1,...,fIndxN}) returns the contents of the specified field, equivalent to value = struct(sIndx1,...,sIndxM).field(fIndx1,...,fIndxN). The getfield function supports multiple sets of field and fIndx inputs, and all Indx inputs are optional. 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. If you specify a single colon operator for an fIndx input, enclose it in single quotation marks: ':'.
> ]
>
> Can someone explains why I can't not get the field of an array:
>
> >> s = struct('f',{1 2})
>
> s =
>
> 1x2 struct array with fields:
> f
>
> >> s(1)
>
> ans =
>
> f: 1
>
> >> s(2)
>
> ans =
>
> f: 2
>
> >> s(1,1:2).f
>
> ans =
>
> 1
>
>
> ans =
>
> 2
>
> >> values = getfield(s,{1,[1 2]}, 'f')
>
> values =
>
> 1
>
> >> [v1 v2] = getfield(s,{1,[1 2]}, 'f')
> Error using getfield
> Too many output arguments.
>
> % MATLAB 2012A
>
> % Bruno

I'll post for the benefit of future readers.

The problem is in getfield.m line 50, where they intentionally return only the first element of the dynamic reference's outputs. Using your example, their choice of syntax would be v1 = s(1,1:2).f. There's an exception underneath it that nearly gets it right, but they pull out only the first element, again. Silly. I was able to use the deal function from the exception code in my case, but there's no guarantee it'll be applicable for all uses.

 - Jason

Tags for this Thread

No tags are associated with 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