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:
How to index into an array of structs?

Subject: How to index into an array of structs?

From: Kirk

Date: 17 Feb, 2012 14:49:12

Message: 1 of 8

I am trying to index into an array of structs for graphing purposes. The data is spatial data in Lat Long format, so to display I use "geoshow(A,cmap,R).

In this case an ozone struct of the array "A" (A.O3), I am trying to do:

geoshow(A(:,:).O3(1,1),cmap,R)

I think this reads "display row1, column1 of all O3 structs in the array A. However, I am getting the error "Bad cell reference operation"

Any suggestions would be much appreciated.

Subject: How to index into an array of structs?

From: Matt J

Date: 17 Feb, 2012 15:07:13

Message: 2 of 8

"Kirk" <kwythers.nospam@umn.edu> wrote in message <jhlpd8$qcf$1@newscl01ah.mathworks.com>...
> I am trying to index into an array of structs for graphing purposes. The data is spatial data in Lat Long format, so to display I use "geoshow(A,cmap,R).
>
> In this case an ozone struct of the array "A" (A.O3), I am trying to do:
>
> geoshow(A(:,:).O3(1,1),cmap,R)
>
> I think this reads "display row1, column1 of all O3 structs in the array A. However, I am getting the error "Bad cell reference operation"
======

So O3 is a struct array? According to the doc, if you'r providing a cmap, the preceding argument is supposed to be numeric, not struct.

Subject: How to index into an array of structs?

From: Kirk

Date: 17 Feb, 2012 16:51:33

Message: 3 of 8

"Matt J" wrote in message <jhlqf1$o6$1@newscl01ah.mathworks.com>...
> "Kirk" <kwythers.nospam@umn.edu> wrote in message <jhlpd8$qcf$1@newscl01ah.mathworks.com>...
> > I am trying to index into an array of structs for graphing purposes. The data is spatial data in Lat Long format, so to display I use "geoshow(A,cmap,R).
> >
> > In this case an ozone struct of the array "A" (A.O3), I am trying to do:
> >
> > geoshow(A(:,:).O3(1,1),cmap,R)
> >
> > I think this reads "display row1, column1 of all O3 structs in the array A. However, I am getting the error "Bad cell reference operation"
> ======
>
> So O3 is a struct array? According to the doc, if you'r providing a cmap, the preceding argument is supposed to be numeric, not struct.


Let's simplify things and start by trying pull the data out array of strucs before I use geoshow. The syntax:

a = A{:,:}.o3(1,1);

gives the error "bad cell reference operation"

and

a = A(:,:).o3(1,1);

gives the error "improper index matrix reference"

Subject: How to index into an array of structs?

From: Matt J

Date: 17 Feb, 2012 17:00:18

Message: 4 of 8

"Kirk" <kwythers.nospam@umn.edu> wrote in message <jhm0il$m7d$1@newscl01ah.mathworks.com>...
>
> Let's simplify things and start by trying pull the data out array of strucs before I use geoshow. The syntax:
>
> a = A{:,:}.o3(1,1);
>
> gives the error "bad cell reference operation"
>
> and
>
> a = A(:,:).o3(1,1);
>
> gives the error "improper index matrix reference"

That's a fair place to start, but you still have to tell us what kind of data type o3 is. If we assume that every o3(1,1) is a numeric scalar, then you can do

a=arrayfun(@(i), A(i).o3(1), 1:numel(A), 'uni',0 );
a=reshape(a,size(A));

Subject: How to index into an array of structs?

From: Kirk

Date: 17 Feb, 2012 17:49:12

Message: 5 of 8

"Matt J" wrote in message <jhm132$nv0$1@newscl01ah.mathworks.com>...
> "Kirk" <kwythers.nospam@umn.edu> wrote in message <jhm0il$m7d$1@newscl01ah.mathworks.com>...
> >
> > Let's simplify things and start by trying pull the data out array of strucs before I use geoshow. The syntax:
> >
> > a = A{:,:}.o3(1,1);
> >
> > gives the error "bad cell reference operation"
> >
> > and
> >
> > a = A(:,:).o3(1,1);
> >
> > gives the error "improper index matrix reference"
>
> That's a fair place to start, but you still have to tell us what kind of data type o3 is. If we assume that every o3(1,1) is a numeric scalar, then you can do
>
> a=arrayfun(@(i), A(i).o3(1), 1:numel(A), 'uni',0 );
> a=reshape(a,size(A));

Thanks Matt J,

OK... upon further review, the array in question is actually a cell array <79x133 cell>, each cell is a <1x1 struct> and each of the structs contain 10 fields <13680x1 double>

So I am attempting to index into one of the fields of a cell array of structs. This was all originally NetCDF data (essentially a big time cube of climate data).

So my attempts like

a = Climin_pcmb1(:,:).O3(1,1) are trying to get at full geographic extent of a single month of ozone data.

Does that help?

Subject: How to index into an array of structs?

From: Matt J

Date: 17 Feb, 2012 18:35:42

Message: 6 of 8

"Kirk" <kwythers.nospam@umn.edu> wrote in message <jhm3uo$46k$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <jhm132$nv0$1@newscl01ah.mathworks.com>...
> > "Kirk" <kwythers.nospam@umn.edu> wrote in message <jhm0il$m7d$1@newscl01ah.mathworks.com>...
> > >
> > > Let's simplify things and start by trying pull the data out array of strucs before I use geoshow. The syntax:
> > >
> > > a = A{:,:}.o3(1,1);
> > >
> > > gives the error "bad cell reference operation"
> > >
> > > and
> > >
> > > a = A(:,:).o3(1,1);
> > >
> > > gives the error "improper index matrix reference"
> >
> > That's a fair place to start, but you still have to tell us what kind of data type o3 is. If we assume that every o3(1,1) is a numeric scalar, then you can do
> >
> > a=arrayfun(@(i), A(i).o3(1), 1:numel(A), 'uni',0 );
> > a=reshape(a,size(A));
>
> Thanks Matt J,
>
> OK... upon further review, the array in question is actually a cell array <79x133 cell>, each cell is a <1x1 struct> and each of the structs contain 10 fields <13680x1 double>
>
> So I am attempting to index into one of the fields of a cell array of structs. This was all originally NetCDF data (essentially a big time cube of climate data).
>
> So my attempts like
>
> a = Climin_pcmb1(:,:).O3(1,1) are trying to get at full geographic extent of a single month of ozone data.
>
> Does that help?
=============

Then what we had before should work, with a minor change,

 a=arrayfun(@(i), A{i}.o3(1), 1:numel(A), 'uni',0 );
 a=reshape(a,size(A));

Subject: How to index into an array of structs?

From: Kirk

Date: 17 Feb, 2012 20:17:29

Message: 7 of 8


> =============
>
> Then what we had before should work, with a minor change,
>
> a=arrayfun(@(i), A{i}.o3(1), 1:numel(A), 'uni',0 );
> a=reshape(a,size(A));

The only thing I have done is to replace the A with the actual name of the cell array "ClimIn_pcmb1, however, I am getting parse error at the fist coma in line 1 and the last parentheses in line 1.

o3_1 = arrayfun(@(i), ClimIn_pcmb1{i}.o3(1), 1:numel(ClimIn_pcmb1), 'uni',0 );
o3_1 = reshape(o3_1, size(ClimIn_pcmb1));

I can not find it.I have stared at this thing for long enough!!! What the heck am I missing?

Subject: How to index into an array of structs?

From: Matt J

Date: 17 Feb, 2012 20:33:17

Message: 8 of 8

"Kirk" <kwythers.nospam@umn.edu> wrote in message <jhmckp$54e$1@newscl01ah.mathworks.com>...
>
> > =============
> >
> > Then what we had before should work, with a minor change,
> >
> > a=arrayfun(@(i), A{i}.o3(1), 1:numel(A), 'uni',0 );
> > a=reshape(a,size(A));
>
> The only thing I have done is to replace the A with the actual name of the cell array "ClimIn_pcmb1, however, I am getting parse error at the fist coma in line 1 and the last parentheses in line 1.
>
> o3_1 = arrayfun(@(i), ClimIn_pcmb1{i}.o3(1), 1:numel(ClimIn_pcmb1), 'uni',0 );
> o3_1 = reshape(o3_1, size(ClimIn_pcmb1));
>
> I can not find it.I have stared at this thing for long enough!!! What the heck am I missing?

The first comma shouldn't be there. This should work (I tested it this time)

o3_1 = arrayfun(@(i) ClimIn_pcmb1{i}.o3(1), 1:numel(ClimIn_pcmb1));
o3_1 = reshape(o3_1, size(ClimIn_pcmb1))

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