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 can one index into a cell array of structs?

Subject: how can one index into a cell array of structs?

From: Kirk

Date: 10 Aug, 2012 13:11:11

Message: 1 of 3

I am trying to produce a map of climate values that are stored in a cell array of structures. For example, from the cell array {}Clim<79x133>, I can retrieve all of a single cell's maximum temperatures (which is a 13680x1 double) with:

Clim{1,1}.tmax

This command gives me all 13680 maximum temperature values from the element tmax. I need to grab a single value for that element for all cells in the array (let's say the last value, number 13680 from all cells)...

I have tried things like

Clim{:,:}.tmax(1360,:)

to no avail...

Can anyone suggest an approach?

Thanks in advance

Subject: how can one index into a cell array of structs?

From: Kirk

Date: 10 Aug, 2012 13:26:08

Message: 2 of 3

"Kirk" <kwythers.nospam@umn.edu> wrote in message <k0319f$okg$1@newscl01ah.mathworks.com>...
> I am trying to produce a map of climate values that are stored in a cell array of structures. For example, from the cell array {}Clim<79x133>, I can retrieve all of a single cell's maximum temperatures (which is a 13680x1 double) with:
>
> Clim{1,1}.tmax
>
> This command gives me all 13680 maximum temperature values from the element tmax. I need to grab a single value for that element for all cells in the array (let's say the last value, number 13680 from all cells)...
>
> I have tried things like
>
> Clim{:,:}.tmax(1360,:)
>
> to no avail...
>
> Can anyone suggest an approach?
>
> Thanks in advance


God am I dense... It seems that I asked this very question 6 months ago. MattJ kindly provided a solution:

tmax_1 = arrayfun(@(i) ClimIn{i}.tmax(1), 1:numel(ClimIn));
tmax_1 = reshape(tmax_1, size(ClimIn));

Works well. Sorry

Subject: how can one index into a cell array of structs?

From: Justin Ashmall

Date: 10 Aug, 2012 13:44:14

Message: 3 of 3

Having a cell array of structs is a bit tricky. Are the structs all different?

If not, having a (regular) array of structs would be much more flexible, if that's possible.

j(1).tmax = rand(100,1);
j(2).tmax = rand(100,1);
j(3).tmax = rand(100,1);
all_tmax = [j.tmax];
size(all_tmax)
  ans =
     100 3

Then use all_tmax as normal. If j (or Clim in your example) is 2d, you might need to use reshape() on all_tmax.

Having said that, if the tmax fields are all different sizes then you're stuck.

Justin


"Kirk" <kwythers.nospam@umn.edu> wrote in message <k0319f$okg$1@newscl01ah.mathworks.com>...
> I am trying to produce a map of climate values that are stored in a cell array of structures. For example, from the cell array {}Clim<79x133>, I can retrieve all of a single cell's maximum temperatures (which is a 13680x1 double) with:
>
> Clim{1,1}.tmax
>
> This command gives me all 13680 maximum temperature values from the element tmax. I need to grab a single value for that element for all cells in the array (let's say the last value, number 13680 from all cells)...
>
> I have tried things like
>
> Clim{:,:}.tmax(1360,:)
>
> to no avail...
>
> Can anyone suggest an approach?
>
> Thanks in advance

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