Memory Requirements for Structure Array
Structure arrays do not require completely contiguous memory. However, each field
requires contiguous memory, as does the header that MATLAB® creates to describe the array. For very large arrays, incrementally increasing
the number of fields or the number of elements in a field results in Out
of
Memory
errors.
Preallocate memory for the contents by assigning initial values with the struct
function, such as
newStruct(1:25,1:50) = struct('a',ones(20),'b',zeros(30),'c',rand(40));
This code creates and populates a 25-by-50 structure array S
with
fields a
, b
, and c
.
If you prefer not to assign initial values, you can initialize a structure array by assigning empty arrays to each field of the last element in the structure array, such as
newStruct(25,50).a = []; newStruct(25,50).b = []; newStruct(25,50).c = [];
or, equivalently,
newStruct(25,50) = struct('a',[],'b',[],'c',[]);
However, in this case, MATLAB only allocates memory for the header, and not for the contents of the array.
For more information, see: