I am hoping to have a multidimensional array of structures, but I can’t seem to get at the field of the contained elements. or in code:
mySample = struct('a', zeros(numA),'b', zeros(numB));
Data = cells(height,width);
disp(Data(1,1).a);
The bottom line fails with an error such as
“Improper index matrix reference.”
How is a 2D array of structures done in Matlab?
There are a couple of ways to create an array of structures (“structure array” or “struct array”). Note that in a struct array, each element must have the same fields. For example, if s(1) has fields “a” and “b”, then s(2)..s(n) must have fields “a” and “b”.
You can expand on that to go beyond the second dimension eaisly:
You could also preallocate the array and use a for-loop to set the value of each field. Additionally:
You could also create a cell array of structures, which provides more flexibility: each structure in the cell array may have different fields.