I am using object-oriented Matlab, and I wonder, what would be the best way to make a list of objects, all instances of the same class? I want to be able to iterate over the list in a for-loop and access the same functions for each instance of my class.
1 Answer
To reiterate what Shai mentioned in the comments, MATLAB supports creating an array of objects, assuming they are all of the same class (and that cat/horzcat/vertcat methods are not explicitly overriden otherwise). For example:
obj = MyClass();
arr = [obj,obj]; %# 1x2 array of objects
for ii=1:numel(arr)
arr(ii)
end
It is even possible to create arrays of objects of different types by implementing converters method or having all your classes inherit from matlab.mixin.Heterogeneous superclass.