How can I create an array of class handles?

109 views (last 30 days)
I'm a C++ developer and need to find an equivalent to a vector of class pointer: std::vector<MyClass*>
I have my Matlab classes inheriting handle already but what would be the appropriate container to use in Matlab for the vector part?
Thanks in advance.

Accepted Answer

Guillaume
Guillaume on 15 Nov 2016
It all depends...
  • Assuming you don't have inheritance, then you can simply create a matrix of your class objects. You can create the array of object any way you want, e.g:
objarray(1:10, 1:5) = MyClass; %creates a 10x5 array initialised with default value of MyClass
objarray(10, 5) = MyClass; %same
objarray(1:10, 1:5) = MyClass(somearg); %creates a 10x5 array initialised with constant MyClass object
objarray = [MyClass, MyClass, MyClass]; %create array by concatenation
objarray = [objarray; MyClass]; %add more elements
Now coming from a C++ background it may not be something you're aware of, but an array of object and a scalar object are exactly the same type and invoke the exact same constructor. In additions all methods of the class can be passed a scalar object or an object array. This is something that may trip you up as you may have designed your class to only work with scalar objects. See this doc and this doc for more on initialising object array, and this one for how to do it in the constructor.
  • If your class is not meant to be used as an array (you can actually prevent array creation), then you could simply store the individual objects in a cell array. The downside is that there is nothing preventing you to store non MyClass in the array.
  • If you have inheritance and MyClass is a base class for several derived classes which you want to be able to store in the array, then you need to derive your base class from matlab.mixin.hetergeneous and implement the required methods.

More Answers (1)

Adam
Adam on 15 Nov 2016
Edited: Adam on 15 Nov 2016
If you mean a vector of class objects all of the same type then you just put them in an object array that behaves like a numeric array:
myObjectHandles = [obj1, obj2, obj3];
etc.
If you want to mix and match classes of a hierarchy that are not all the same type you have to do a bit more work, but unless that is what you mean I won't clutter my answer with that to start with.
Object arrays do have some nice properties. For example if your class has a property 'value' you can type:
myValues = [myObjectHandles.value];
to get an array of the 'value' property from each of the objects in your array also.
  2 Comments
Guillaume
Guillaume on 15 Nov 2016
Edited: Guillaume on 15 Nov 2016
"Object arrays do have some nice properties". Yes, however object arrays is also something that can trip people coming from other languages where an array of object and a scalar object are two completely different types.
In matlab, myObjectHandles is a 1x3 MyClass array, while obj1 is a 1x1 MyClass object. They both have exactly the same methods, and when you invoke any method of MyClass (e.g. MyMethod(obj, args)), in the first case, obj will be a 1x3 MyClass object, while in the 2nd it will be a 1x1 MyClass object. This is something that will cause problems (it did for me anyway) if you write your method the same as in C++ where the this object is always scalar.
Nowadays, I tend to prevent array of object creation / concatenation in my class by default and only allow it when I've made the explicit effort of making sure my class methods all work with non-scalar objects.
Adam
Adam on 15 Nov 2016
Yeah, I use object arrays for storage, but I don't support them in every function of the class as that is just too much work. I am happy to just use a for loop for that even though I know a for loop outside of a class is less efficient than one inside potentially (depending on how it is coded), but I've only found that to matter in a handful of cases where speed is really critical.

Sign in to comment.

Categories

Find more on Handle Classes in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!