"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 preallocate by doing
> >
> > S(bigNum).x=[];
> > S(bigNum).y=[];
> >
> > However, you should be advised that, even with preallocation, 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.
