I am trying to preallocate an object array of a class. Below is the class that I am using in MyobjectArray class
classdef MyData properties x = []; % a column vector of data
end methods function obj = MyData(nRows) if nRows > 0 obj.x = zeros(nRows,1); end end endend
In the code below I am using obj.value = zeros(nArrays,1); to preallocate. What is the correct way to preallocate an object array?
classdef MyobjectArray properties value = []; % an array of MyData objects
end methods function obj = MyobjectArray(nArrays, nRows) if nargin ~= 0 obj.value = zeros(nArrays,1); for i = 1:nArrays obj(i).value = MyData(nRows); end end end endend
Best Answer