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:
Initialize a structure?

Subject: Initialize a structure?

From: Chuck37

Date: 15 Oct, 2012 23:48:16

Message: 1 of 6

I need a structure that looks like:

S(ii).x(jj)
S(ii).y(jj)

where the range of ii is fixed but can be very large. jj can also be a fixed size.

I've tried to pre-allocate S, but M-lint doesn't see to agree and tells me that S appears to change size. Here's how I'm trying to do it:

Stemp.x = zeros(1,40);
Stemp.y = zeros(1,40);

for ii = bigNum:-1:1
  S(ii) = Stemp;
end

I went backwards to make the process go faster. Matlab appears to fill in quickly everything below an index you put in...

Later on:

for ii = 1:bigNum
  for jj = 1:smallNum
    S(ii).x(jj) = someValue;
  end
end

Matlab gives me the warning here about S appearing to grow, and the loop takes forever to complete, as though memory is being dealt with inefficiently.

Any ideas?

Subject: Initialize a structure?

From: TideMan

Date: 16 Oct, 2012 02:13:49

Message: 2 of 6

On Tuesday, October 16, 2012 12:48:17 PM UTC+13, Chuck37 wrote:
> I need a structure that looks like:
>
>
>
> S(ii).x(jj)
>
> S(ii).y(jj)
>
>
>
> where the range of ii is fixed but can be very large. jj can also be a fixed size.
>
>
>
> I've tried to pre-allocate S, but M-lint doesn't see to agree and tells me that S appears to change size. Here's how I'm trying to do it:
>
>
>
> Stemp.x = zeros(1,40);
>
> Stemp.y = zeros(1,40);
>
>
>
> for ii = bigNum:-1:1
>
> S(ii) = Stemp;
>
> end
>
>
>
> I went backwards to make the process go faster. Matlab appears to fill in quickly everything below an index you put in...
>
>
>
> Later on:
>
>
>
> for ii = 1:bigNum
>
> for jj = 1:smallNum
>
> S(ii).x(jj) = someValue;
>
> end
>
> end
>
>
>
> Matlab gives me the warning here about S appearing to grow, and the loop takes forever to complete, as though memory is being dealt with inefficiently.
>
>
>
> Any ideas?

Why are you bothering?
The way structures work, you only need to preallocate the fields, and even then the saving is tiny.
This has been discussed ad nauseum in this forum.

Subject: Initialize a structure?

From: Chuck37

Date: 16 Oct, 2012 03:42:11

Message: 3 of 6

TideMan <mulgor@gmail.com> wrote in message <713169fb-e423-4479-8698-14aebd1309aa@googlegroups.com>...
> Why are you bothering?
> The way structures work, you only need to preallocate the fields, and even then the saving is tiny.
> This has been discussed ad nauseum in this forum.

I'm bothering because it takes a very long time to fill it in a million times if its not pre-allocated. Doing what I did made a difference, but since the editor is still complaining, I wonder if I'm still not doing all that can be done. It's taking minutes to fill it in and the computations involved are trivial, so it must be a memory scrounging problem.

Subject: Initialize a structure?

From: Matt J

Date: 16 Oct, 2012 04:55:13

Message: 4 of 6

"Chuck37" wrote in message <k5i7c0$1mr$1@newscl01ah.mathworks.com>...
> I need a structure that looks like:
>
> Later on:
>
> for ii = 1:bigNum
> for jj = 1:smallNum
> S(ii).x(jj) = someValue;
> end
> end
>
> Matlab gives me the warning here about S appearing to grow, and the loop takes forever to complete, as though memory is being dealt with inefficiently.
========

You can pre-allocate by doing

S(bigNum).x=[];
S(bigNum).y=[];

However, you should be advised that, even with pre-allocation, structures are a very inefficient way to store data of the kind you describe. Structure elements, unlike numeric arrays, are not stored contiguously in memory, which makes them slow to access and the problem gets worse the larger the structure array. Because all your x and y are the same, it makes more sense (and will be faster) to store them in matrices of dimension bigNum x smallNum.

Subject: Initialize a structure?

From: Chuck37

Date: 16 Oct, 2012 14:40:09

Message: 5 of 6

"Matt J" wrote in message <k5ipbh$lk$1@newscl01ah.mathworks.com>...
> "Chuck37" wrote in message <k5i7c0$1mr$1@newscl01ah.mathworks.com>...
> > I need a structure that looks like:
> >
> > Later on:
> >
> > for ii = 1:bigNum
> > for jj = 1:smallNum
> > S(ii).x(jj) = someValue;
> > end
> > end
> >
> > Matlab gives me the warning here about S appearing to grow, and the loop takes forever to complete, as though memory is being dealt with inefficiently.
> ========
>
> You can pre-allocate by doing
>
> S(bigNum).x=[];
> S(bigNum).y=[];
>
> However, you should be advised that, even with pre-allocation, structures are a very inefficient way to store data of the kind you describe. Structure elements, unlike numeric arrays, are not stored contiguously in memory, which makes them slow to access and the problem gets worse the larger the structure array. Because all your x and y are the same, it makes more sense (and will be faster) to store them in matrices of dimension bigNum x smallNum.

Thanks, I'll think about how to do that. I, of course, simplified the real situation for the post. Each element actually has 4 vector elements and some metadata that would need to be store separately if I switched away from the structure. So I think what you are suggesting implies sending at least five things to all my support functions instead of one (assuming I still keep metadata in structure form).

Subject: Initialize a structure?

From: Matt J

Date: 17 Oct, 2012 07:54:19

Message: 6 of 6

"Chuck37" wrote in message <k5jrk9$mi$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <k5ipbh$lk$1@newscl01ah.mathworks.com>...
> > "Chuck37" wrote in message <k5i7c0$1mr$1@newscl01ah.mathworks.com>...
> > > I need a structure that looks like:
> > >
> > > Later on:
> > >
> > > for ii = 1:bigNum
> > > for jj = 1:smallNum
> > > S(ii).x(jj) = someValue;
> > > end
> > > end
> > >
> > > Matlab gives me the warning here about S appearing to grow, and the loop takes forever to complete, as though memory is being dealt with inefficiently.
> > ========
> >
> > You can pre-allocate by doing
> >
> > S(bigNum).x=[];
> > S(bigNum).y=[];
> >
> > However, you should be advised that, even with pre-allocation, structures are a very inefficient way to store data of the kind you describe. Structure elements, unlike numeric arrays, are not stored contiguously in memory, which makes them slow to access and the problem gets worse the larger the structure array. Because all your x and y are the same, it makes more sense (and will be faster) to store them in matrices of dimension bigNum x smallNum.
>
> Thanks, I'll think about how to do that. I, of course, simplified the real situation for the post. Each element actually has 4 vector elements and some metadata that would need to be store separately if I switched away from the structure. So I think what you are suggesting implies sending at least five things to all my support functions instead of one (assuming I still keep metadata in structure form).

No, because even though you would be migrating the 4 vector elements to 4 matrices, you can still package those 4 matrices in a second, scalar structure. Scalar structures are not as scattered in memory as structure arrays. So you would only have 2 things to pass around. You could also have a struct array inside a scalar struct:

scalarstruct.x=...
scalarstruct.y=...
scalarstruct.S=...

where S is your original, metadata struct array with x, y and the other 2 vector elements removed. It really all just depends on what you need fast access to.

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