MATLAB: Pre-allocating structures

preallocationstructures

Hello everyone,
I'm trying to pre-allocate memory for a structure defined as follows:
% S : Structure containing geometrical description of polygons.
% S(i) contains all information relative to the i-th shape
% S(i).P(j) gives access to the geometrical description of the j-th
% element of the i-th shape.
% XData : S(i).P(j).x : Vector
% YData : S(i).P(j).y : Vector
% Hole : S(i).P(j).hole : Binary value
I'd like to pre-allocate before the following loop:
for i=1:ne
for j=1:2
S(i).P(j).x = v_proj(f(i,j),1);
S(i).P(j).y = v_proj(f(i,j),2);
S(i).P(j).hole = 0;
end
end
I'd like to have a number of shapes ne.
But I don't really know how to do that. Any help would be more than welcome.
Thank you very much,
% Romain

Best Answer

One way to preallocate:
P(1:2) = struct('x',zeros(10,1),'y',zeros(1,2),'hole',false);
ne = 10;
[S(1:ne).P] = deal(P);