I have read the advice on how to avoid inefficient use of memory by preallocation of matrices using zeros for eg. I am having trouble translating that into how to preallocate memory for a structure which has various fields, including some matrices. The structure has to be created a field at a time. But I want to avoid "growing" it and wasting memory.
Could I create a huge array the size of the final structure, and then just start creating structure fields using that matrix name, and avoid thereby duplicate things on the heap while the structure is being built.
If not what is recommended Details appreciated. (Not using Windows, So memory command not available).
No products are associated with this question.
Stucture_Size = 78965; %number of elements 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'); %completely different type is OK!
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); %legal but often useless
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 .