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:
shaping structs

Subject: shaping structs

From: Kirk

Date: 15 Apr, 2011 16:29:04

Message: 1 of 7

I have a struct that I want to reshape. The structure has the shape of 50 fields that are 3600x1. The 3600 row column represents 300 years of monthly climate data. I want to reshape all 50 3600x1 fields into 50 12x300 fields (12 months by 300 years), so that I can do perform some statistics on each year's elements. However, I keep getting the error"

??? Error using ==> reshape
To RESHAPE the number of elements must not change.

Error in ==> out_yr2000_2200_50i at 61
dlclim1xs = reshape(dlclim1xs,12,300);

I can reshape each filed individually, but that kinda defeats the purpose of placing the data into a struct in the fist place

dlclim1xs.tmax1 = reshape(dlclim1xs.tmax1,12,300);
dlclim1xs.tmax2 = reshape(dlclim1xs.tmax2,12,300);
dlclim1xs.tmax3 = reshape(dlclim1xs.tmax3,12,300);
dlclim1xs.tmax4 = reshape(dlclim1xs.tmax4,12,300);
dlclim1xs.tmax5 = reshape(dlclim1xs.tmax5,12,300);
etc...

Is there an approach that will let me reshape the whole struct "dlclim1xs" at once?

Thanks

Subject: shaping structs

From: Steven_Lord

Date: 15 Apr, 2011 17:19:57

Message: 2 of 7



"Kirk" <kwythers.nospam@umn.edu> wrote in message
news:io9rog$b64$1@fred.mathworks.com...
> I have a struct that I want to reshape. The structure has the shape of 50
> fields that are 3600x1. The 3600 row column represents 300 years of
> monthly climate data. I want to reshape all 50 3600x1 fields into 50
> 12x300 fields (12 months by 300 years), so that I can do perform some
> statistics on each year's elements. However, I keep getting the error"
>
> ??? Error using ==> reshape
> To RESHAPE the number of elements must not change.
>
> Error in ==> out_yr2000_2200_50i at 61
> dlclim1xs = reshape(dlclim1xs,12,300);
>
> I can reshape each filed individually, but that kinda defeats the purpose
> of placing the data into a struct in the fist place
> dlclim1xs.tmax1 = reshape(dlclim1xs.tmax1,12,300);
> dlclim1xs.tmax2 = reshape(dlclim1xs.tmax2,12,300);
> dlclim1xs.tmax3 = reshape(dlclim1xs.tmax3,12,300);
> dlclim1xs.tmax4 = reshape(dlclim1xs.tmax4,12,300);
> dlclim1xs.tmax5 = reshape(dlclim1xs.tmax5,12,300);
> etc...
>
> Is there an approach that will let me reshape the whole struct "dlclim1xs"
> at once?

Your struct array is 1-by-1 and you can't turn a 1-by-1 into a 12-by-300.
The _fields_ in your struct array may contain matrices with 3600 elements,
but your struct array itself does not.

For this, I would use STRUCTFUN or a simple FOR loop to reshape the contents
of each field.

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

Subject: shaping structs

From: Kirk

Date: 9 May, 2011 15:27:04

Message: 3 of 7

"Steven_Lord" <slord@mathworks.com> wrote in message <io9ung$mc7$1@fred.mathworks.com>...
>
>
> "Kirk" <kwythers.nospam@umn.edu> wrote in message
> news:io9rog$b64$1@fred.mathworks.com...
> > I have a struct that I want to reshape. The structure has the shape of 50
> > fields that are 3600x1. The 3600 row column represents 300 years of
> > monthly climate data. I want to reshape all 50 3600x1 fields into 50
> > 12x300 fields (12 months by 300 years), so that I can do perform some
> > statistics on each year's elements. However, I keep getting the error"
> >
> > ??? Error using ==> reshape
> > To RESHAPE the number of elements must not change.
> >
> > Error in ==> out_yr2000_2200_50i at 61
> > dlclim1xs = reshape(dlclim1xs,12,300);
> >
> > I can reshape each filed individually, but that kinda defeats the purpose
> > of placing the data into a struct in the fist place
> > dlclim1xs.tmax1 = reshape(dlclim1xs.tmax1,12,300);
> > dlclim1xs.tmax2 = reshape(dlclim1xs.tmax2,12,300);
> > dlclim1xs.tmax3 = reshape(dlclim1xs.tmax3,12,300);
> > dlclim1xs.tmax4 = reshape(dlclim1xs.tmax4,12,300);
> > dlclim1xs.tmax5 = reshape(dlclim1xs.tmax5,12,300);
> > etc...
> >
> > Is there an approach that will let me reshape the whole struct "dlclim1xs"
> > at once?
>
> Your struct array is 1-by-1 and you can't turn a 1-by-1 into a 12-by-300.
> The _fields_ in your struct array may contain matrices with 3600 elements,
> but your struct array itself does not.
>
> For this, I would use STRUCTFUN or a simple FOR loop to reshape the contents
> of each field.

Steve,

Thanks for the reply. I am trying to set up a for loop that steps through each column of the variable "tmax" and places a subset of those data into a struct. I have managed to define the data subset as rows 11989:13788, and columns 1:5. However each time through the loop, I end up overwriting the data into column 1. Here is my code:

% Load climate variables (tmax, tmin, par, prec)
load ../../_output/ra1000/dl/full/aspn49xsClim.mat

% pull out subset of years 2001-2200 (rows12001:13800), for the first 5
% iterations (columns 1:5).
for n = tmax(11989:13788,1:5)
    dlclim49xs1.tmax = n;
end;

How can I get all 5 column into the struc "dlclim49xs.tamx"? Thanks... this is driving me crazy!

Subject: shaping structs

From: Kirk

Date: 9 May, 2011 15:35:06

Message: 4 of 7


> Steve,
>
> Thanks for the reply. I am trying to set up a for loop that steps through each column of the variable "tmax" and places a subset of those data into a struct. I have managed to define the data subset as rows 11989:13788, and columns 1:5. However each time through the loop, I end up overwriting the data into column 1. Here is my code:
>
> % Load climate variables (tmax, tmin, par, prec)
> load ../../_output/ra1000/dl/full/aspn49xsClim.mat
>
> % pull out subset of years 2001-2200 (rows12001:13800), for the first 5
> % iterations (columns 1:5).
> for n = tmax(11989:13788,1:5)
> dlclim49xs1.tmax = n;
> end;
>
> How can I get all 5 column into the struc "dlclim49xs.tamx"? Thanks... this is driving me crazy!

I'm trying to avoid building the struct with something like this:

dlclim49xs.tmax1 = tmax(11989:13788,1);
dlclim49xs.tmax2 = tmax(11989:13788,2);
dlclim49xs.tmax3 = tmax(11989:13788,3);
dlclim49xs.tmax4 = tmax(11989:13788,4);
dlclim49xs.tmax5 = tmax(11989:13788,5);

Subject: shaping structs

From: Steven_Lord

Date: 9 May, 2011 17:17:54

Message: 5 of 7



"Kirk" <kwythers.nospam@umn.edu> wrote in message
news:iq91ja$50p$1@newscl01ah.mathworks.com...
>
>> Steve,
>>
>> Thanks for the reply. I am trying to set up a for loop that steps through
>> each column of the variable "tmax" and places a subset of those data into
>> a struct. I have managed to define the data subset as rows 11989:13788,
>> and columns 1:5. However each time through the loop, I end up overwriting
>> the data into column 1. Here is my code:
>>
>> % Load climate variables (tmax, tmin, par, prec)
>> load ../../_output/ra1000/dl/full/aspn49xsClim.mat
>>
>> % pull out subset of years 2001-2200 (rows12001:13800), for the first 5 %
>> iterations (columns 1:5).
>> for n = tmax(11989:13788,1:5)
>> dlclim49xs1.tmax = n;
>> end;
>>
>> How can I get all 5 column into the struc "dlclim49xs.tamx"? Thanks...
>> this is driving me crazy!
>
> I'm trying to avoid building the struct with something like this:
>
> dlclim49xs.tmax1 = tmax(11989:13788,1);
> dlclim49xs.tmax2 = tmax(11989:13788,2);
> dlclim49xs.tmax3 = tmax(11989:13788,3);
> dlclim49xs.tmax4 = tmax(11989:13788,4);
> dlclim49xs.tmax5 = tmax(11989:13788,5);

I would personally put the entire submatrix into _one_ field in your struct
and extract the appropriate columns as necessary, to keep the number of
different variable names to a minimum. The fewer things you have to remember
the better.

dlclim49xs.tmax = tmax(11989:13788, 1:5);
% now use dlclim49xs.tmax(:, 3) instead of dlclim49xs.tmax3

But if you must do this, you can use dynamic field names.

for k = 1:5
    theField = sprintf('tmax%d', k);
    dlclim49xs.(theField) = tmax(11989:13788, k);
end

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

Subject: shaping structs

From: Kirk

Date: 10 May, 2011 12:45:05

Message: 6 of 7

> I would personally put the entire submatrix into _one_ field in your struct
> and extract the appropriate columns as necessary, to keep the number of
> different variable names to a minimum. The fewer things you have to remember
> the better.
>
> dlclim49xs.tmax = tmax(11989:13788, 1:5);
> % now use dlclim49xs.tmax(:, 3) instead of dlclim49xs.tmax3
>
> But if you must do this, you can use dynamic field names.
>
> for k = 1:5
> theField = sprintf('tmax%d', k);
> dlclim49xs.(theField) = tmax(11989:13788, k);
> end
>

Thanks Steve. I wil follow your first suggestion, since in this case, I can keep is simple. However, I am intrigued by your "dynamic field names" suggestion. Since I may very well need to use this approach in the future, I was wondering if you could provide a little more detail, particularly the use of sprintf().

Subject: shaping structs

From: Steven_Lord

Date: 10 May, 2011 14:19:59

Message: 7 of 7



"Kirk" <kwythers.nospam@umn.edu> wrote in message
news:iqbc0h$4ut$1@newscl01ah.mathworks.com...
>> I would personally put the entire submatrix into _one_ field in your
>> struct and extract the appropriate columns as necessary, to keep the
>> number of different variable names to a minimum. The fewer things you
>> have to remember the better.
>>
>> dlclim49xs.tmax = tmax(11989:13788, 1:5);
>> % now use dlclim49xs.tmax(:, 3) instead of dlclim49xs.tmax3
>>
>> But if you must do this, you can use dynamic field names.
>>
>> for k = 1:5
>> theField = sprintf('tmax%d', k);
>> dlclim49xs.(theField) = tmax(11989:13788, k);
>> end
>>
>
> Thanks Steve. I wil follow your first suggestion, since in this case, I
> can keep is simple. However, I am intrigued by your "dynamic field names"
> suggestion. Since I may very well need to use this approach in the future,
> I was wondering if you could provide a little more detail, particularly
> the use of sprintf().

I just used SPRINTF to construct a string; you could just as easily have
used:

theField = ['tmax' num2str(k)];

As for information about dynamic field name indexing:

http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/
http://blogs.mathworks.com/videos/2008/08/20/advanced-matlab-dynamic-field-names/
http://blogs.mathworks.com/videos/2009/02/27/dynamic-field-name-usage/

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

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