Like this:
Stucture_Size = 78965;
StructureName(Structure_Size) = struct();
Then StructureName will become a structure array of the proper size, starting with no fields.
As you add a previously-unused field to any element in the structure array, the field will be added to every element in the structure array, initialized to the empty double array in those other structure array elements. For example, if you set
StructureName(1).MyNewField = 'A' : 'Q';
then StructureName(483).MyNewField would pop into existence as the empty double array, not as an empty char array.
Each structure array member can have its field be a different type.
StructureName(1).MyNewField = 'A' : 'Q';
StructureName(2).MyNewField = serial('COM3');
When you make an assignment to all of an ordinary MATLAB variable, or you make an assignment to all of a particular structure array field name, then the memory used for the expression on the right hand side is shared with the left-hand side, not copied (but memory is copied if you assign to part of a variable, such as assigning to Q(8) = 17 ). Because of this, most often you do not need to preallocate the memory for a structure array member field name:
StructureName(278).MyNewField = zeros(1,500);
In particular the preallocation would be useless if you then assign to all of the member's field, such as
StructureName(278).MyNewField = rand(1,500);
It would not, however, be useless if you immediately start working with just part of the variable, such as
StructureName(278).MyNewField(43:59) = 0.5;
In a numeric array, if you had created the first 277 rows then assigning the 278'th row would be inefficient, but in structure arrays as long as you have pre-allocated the structure array itself, there is no inefficiency for having created values only for StructureName(1:277).MyNewField and not having preallocated for StructureName(278).MyNewField .
Best Answer