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:
Preallocate Structure Array with Fields

Subject: Preallocate Structure Array with Fields

From: John Baillie

Date: 7 Aug, 2012 08:52:16

Message: 1 of 5

Hello,

I am copying large amounts of data to a structure array:
data_voltage<1x3> structure
Each of the nine cells contains a structure with two fields, x and y, for example:
data_voltage(1,1).x
data_voltage(1,3).y
Each field will contain a matrix of 5e6 rows and 1 column, for example:
data_voltage(1,1).x(5e6,1)=3.9467

Since I am copying my data from fields in a for loop it is necesary that I preallocate the cells to avoid memory issues. I have done this as follows:
data_voltage=struct('x',{zeros(5e6,1), zeros(5e6,1), zeros(5e6,1),}, 'y',{zeros(5e6,1), zeros(5e6,1), zeros(5e6,1)});

Is there a way to improve this code so that i do not need to repeat the line zeros(5e6,1) 3 times? I have tried using the cell command but to no avail.

Thanks,
John

Subject: Preallocate Structure Array with Fields

From: Bruno Luong

Date: 7 Aug, 2012 09:06:15

Message: 2 of 5

a = zeros(1e2,1);
s1 = struct('x',a,'y',a,'z',a);
s = repmat(s1,[1 3]);

Not that there is only one copy of A allocated at this point. Other copies will be allocated while the fields are set later on the code (copy-on-write method).

Bruno

Subject: Preallocate Structure Array with Fields

From: Barry Williams

Date: 7 Aug, 2012 09:10:20

Message: 3 of 5

"John Baillie" wrote in message <jvql00$3k2$1@newscl01ah.mathworks.com>...
> Hello,
>
> I am copying large amounts of data to a structure array:
> data_voltage<1x3> structure
> Each of the nine cells contains a structure with two fields, x and y, for example:
> data_voltage(1,1).x
> data_voltage(1,3).y
> Each field will contain a matrix of 5e6 rows and 1 column, for example:
> data_voltage(1,1).x(5e6,1)=3.9467
>
> Since I am copying my data from fields in a for loop it is necesary that I preallocate the cells to avoid memory issues. I have done this as follows:
> data_voltage=struct('x',{zeros(5e6,1), zeros(5e6,1), zeros(5e6,1),}, 'y',{zeros(5e6,1), zeros(5e6,1), zeros(5e6,1)});
>
> Is there a way to improve this code so that i do not need to repeat the line zeros(5e6,1) 3 times? I have tried using the cell command but to no avail.
>
> Thanks,
> John

data_voltage=struct('x',repmat({zeros(5e6,1)},1,3), 'y',repmat({zeros(5e6,1)},1,3);

Barry

Subject: Preallocate Structure Array with Fields

From: Bruno Luong

Date: 7 Aug, 2012 09:55:16

Message: 4 of 5

Another way:

a = num2cell(zeros(100,3),1)
s = struct('x',a,'y',a,'z',a)

% Bruno

Subject: Preallocate Structure Array with Fields

From: James Tursa

Date: 7 Aug, 2012 22:10:23

Message: 5 of 5

"John Baillie" wrote in message <jvql00$3k2$1@newscl01ah.mathworks.com>...
> Hello,
>
> I am copying large amounts of data to a structure array:
> data_voltage<1x3> structure
> Each of the nine cells contains a structure with two fields, x and y, for example:
> data_voltage(1,1).x
> data_voltage(1,3).y
> Each field will contain a matrix of 5e6 rows and 1 column, for example:
> data_voltage(1,1).x(5e6,1)=3.9467
>
> Since I am copying my data from fields in a for loop it is necesary that I preallocate the cells to avoid memory issues. I have done this as follows:
> data_voltage=struct('x',{zeros(5e6,1), zeros(5e6,1), zeros(5e6,1),}, 'y',{zeros(5e6,1), zeros(5e6,1), zeros(5e6,1)});
>
> Is there a way to improve this code so that i do not need to repeat the line zeros(5e6,1) 3 times? I have tried using the cell command but to no avail.

I will emphasize what Bruno already stated: There is no advantage to using the *same* zeros matrix for your preallocation since each copy will get reallocated and copied in its entirety anyway downstream when you start replacing individual elements. But not much harm done either.

James Tursa

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