Got Questions? Get Answers.
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:
indexing into a struct of structs

Subject: indexing into a struct of structs

From: Kirk

Date: 20 Jul, 2011 06:50:13

Message: 1 of 7

I have a <1 x 1000> struct of <1 x 1> structs. I am trying to index into the first field of each <1 x 1> struct for all 1000 structs in the initial struct.

For example, I can access the first 12 elements of the first struct of the first field with:

struct(1,1).field(1:12,1);

However, if I try and grab the same column of data for all 1000 structs with:

struct(1,:).field(1:12,1);

I get the message:

??? scalar index required for this type of multi-level indexing.

Any advice on how to approach this problem would be appreciated. Thanks.

Subject: indexing into a struct of structs

From: Matt J

Date: 20 Jul, 2011 17:30:26

Message: 2 of 7

"Kirk" <kwythers.nospam@umn.edu> wrote in message <j05tr5$hir$1@newscl01ah.mathworks.com>...
>
> Any advice on how to approach this problem would be appreciated. Thanks.
==================

One way:

arrayfun(@(s) s.field(1:12,1), struct,'uni',0)

Subject: indexing into a struct of structs

From: Kirk

Date: 20 Jul, 2011 20:05:10

Message: 3 of 7

"Matt J" wrote in message <j073bi$3ur$1@newscl01ah.mathworks.com>...
> "Kirk" <kwythers.nospam@umn.edu> wrote in message <j05tr5$hir$1@newscl01ah.mathworks.com>...
> >
> > Any advice on how to approach this problem would be appreciated. Thanks.
> ==================
>
> One way:
>
> arrayfun(@(s) s.field(1:12,1), struct,'uni',0)

Thanks for the reply Matt. Can you walk me through what is going on in this code? I can recognize the s.field(1:12:1) as rows 1 - 12 in column one, from the field in question of the original struct (s). However, I am not following the (@(s) bits and the ,stuct,'uni'0) bits.

Thanks

Subject: indexing into a struct of structs

From: Matt J

Date: 20 Jul, 2011 21:39:09

Message: 4 of 7

"Kirk" <kwythers.nospam@umn.edu> wrote in message <j07cdm$2c7$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <j073bi$3ur$1@newscl01ah.mathworks.com>...
> > "Kirk" <kwythers.nospam@umn.edu> wrote in message <j05tr5$hir$1@newscl01ah.mathworks.com>...
> > >
> > > Any advice on how to approach this problem would be appreciated. Thanks.
> > ==================
> >
> > One way:
> >
> > arrayfun(@(s) s.field(1:12,1), struct,'uni',0)
>
> Thanks for the reply Matt. Can you walk me through what is going on in this code? I can recognize the s.field(1:12:1) as rows 1 - 12 in column one, from the field in question of the original struct (s).

>However, I am not following the (@(s) bits
========

You need to get familiar with anonymous functions. To get the idea, try this set of commands at the MATLAB prompt
>> f=@(x) x.^2

f =

    @(x)x.^2

>> f(2)

ans =

     4

>> f(3)

ans =

     9

>> f(4)

ans =

    16




> and the ,stuct,'uni'0) bits.
========

ARRAYFUN will apply the anonymous function

fun=@(s) s.field(1:12,1)

to each struct(i) in the array you've called struct.

The arguments {'UniformOutput',0}
which I've abbreviated to {'uni',0} tells arrayfun that the outputs of fun won't be scalars and need to be kept in a cell array.

Subject: indexing into a struct of structs

From: Kirk

Date: 20 Jul, 2011 22:43:25

Message: 5 of 7

"Kirk" <kwythers.nospam@umn.edu> wrote in message <j07cdm$2c7$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <j073bi$3ur$1@newscl01ah.mathworks.com>...
> > "Kirk" <kwythers.nospam@umn.edu> wrote in message <j05tr5$hir$1@newscl01ah.mathworks.com>...
> > >
> > > Any advice on how to approach this problem would be appreciated. Thanks.
> > ==================
> >
> > One way:
> >
> > arrayfun(@(s) s.field(1:12,1), struct,'uni',0)
>
> Thanks for the reply Matt. Can you walk me through what is going on in this code? I can recognize the s.field(1:12:1) as rows 1 - 12 in column one, from the field in question of the original struct (s). However, I am not following the (@(s) bits and the ,stuct,'uni'0) bits.
>
> Thanks

Thank. This works:

>> arrayfun(@(s) s.tmax(1:12,1), climIn_mp_49x(1,1:100),'uni',0);

It gives me a 1 x 100 cell array of 12 tmax values. Is there a way to convert a cell array into simple matrix of 12 rows x 100 columns?

Subject: indexing into a struct of structs

From: Matt J

Date: 20 Jul, 2011 23:01:26

Message: 6 of 7

"Kirk" <kwythers.nospam@umn.edu> wrote in message <j07lmd$qop$1@newscl01ah.mathworks.com>...
> "Kirk" <kwythers.nospam@umn.edu> wrote in message <j07cdm$2c7$1@newscl01ah.mathworks.com>...
> > "Matt J" wrote in message <j073bi$3ur$1@newscl01ah.mathworks.com>...
> > > "Kirk" <kwythers.nospam@umn.edu> wrote in message <j05tr5$hir$1@newscl01ah.mathworks.com>...
> > > >
> > > > Any advice on how to approach this problem would be appreciated. Thanks.
> > > ==================
> > >
> > > One way:
> > >
> > > arrayfun(@(s) s.field(1:12,1), struct,'uni',0)
> >
> > Thanks for the reply Matt. Can you walk me through what is going on in this code? I can recognize the s.field(1:12:1) as rows 1 - 12 in column one, from the field in question of the original struct (s). However, I am not following the (@(s) bits and the ,stuct,'uni'0) bits.
> >
> > Thanks
>
> Thank. This works:
>
> >> arrayfun(@(s) s.tmax(1:12,1), climIn_mp_49x(1,1:100),'uni',0);
>
> It gives me a 1 x 100 cell array of 12 tmax values. Is there a way to convert a cell array into simple matrix of 12 rows x 100 columns?


See documentation for CELL2MAT.

You can also do it by concatenation, e.g.,

>> c={1,2,3}; concat=[c{:}]

concat =

     1 2 3

Subject: indexing into a struct of structs

From: Kirk

Date: 20 Jul, 2011 23:10:10

Message: 7 of 7

"Matt J" wrote in message <j07mo6$3h$1@newscl01ah.mathworks.com>...
> "Kirk" <kwythers.nospam@umn.edu> wrote in message <j07lmd$qop$1@newscl01ah.mathworks.com>...
> > "Kirk" <kwythers.nospam@umn.edu> wrote in message <j07cdm$2c7$1@newscl01ah.mathworks.com>...
> > > "Matt J" wrote in message <j073bi$3ur$1@newscl01ah.mathworks.com>...
> > > > "Kirk" <kwythers.nospam@umn.edu> wrote in message <j05tr5$hir$1@newscl01ah.mathworks.com>...
> > > > >
> > > > > Any advice on how to approach this problem would be appreciated. Thanks.
> > > > ==================
> > > >
> > > > One way:
> > > >
> > > > arrayfun(@(s) s.field(1:12,1), struct,'uni',0)
> > >
> > > Thanks for the reply Matt. Can you walk me through what is going on in this code? I can recognize the s.field(1:12:1) as rows 1 - 12 in column one, from the field in question of the original struct (s). However, I am not following the (@(s) bits and the ,stuct,'uni'0) bits.
> > >
> > > Thanks
> >
> > Thank. This works:
> >
> > >> arrayfun(@(s) s.tmax(1:12,1), climIn_mp_49x(1,1:100),'uni',0);
> >
> > It gives me a 1 x 100 cell array of 12 tmax values. Is there a way to convert a cell array into simple matrix of 12 rows x 100 columns?
>
>
> See documentation for CELL2MAT.
>
> You can also do it by concatenation, e.g.,
>
> >> c={1,2,3}; concat=[c{:}]
>
> concat =
>
> 1 2 3


yes... I just found cell2mat. Thank you.

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