3

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
  • 7
    what's wrong with a simple array? Commented Jun 26, 2013 at 11:17

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.