3

In Matlab I have three classes. The parent class is an abstract class named Zone. It has two child classes that inherit from it. They are IZone and SZone.

I have a fourth class called System that is supposed to have an array of Zone objects. However, when I create the array and pass in an SZone object, the array becomes an array of type SZone and Matlab won't let me add any IZone objects to it.

If I add an IZone and SZone at the same time, it creates an array of type Zone. But I never actually do this. I only add one zone at a time. Is there some way to cast the array to a type of Zone? Like in Java?

Right now I'm considering changing the array to a cell array. But I'd prefer to keep this as an array of objects of type Zone.

Note: Zone doesn't have to be Abstract.

EDIT

I found a similar question here about specifically abstract classes. So this question might be a duplicate.

How do I create an array of abstract class objects in MATLAB?

Community
  • 1
  • 1
toshiomagic
  • 1,335
  • 1
  • 12
  • 39
  • I think my only option here is to create another object called ZoneArray that overloads the subsref function. I'd really really really rather not have to do that though. It seems like I'd have to use a cell array inside that object, and then I might as well just use a cell array. – toshiomagic Mar 23 '16 at 14:40
  • I have flagged my own question as a duplicate. The matlab.mixin.Heterogeneous solution worked! – toshiomagic Mar 23 '16 at 14:55

0 Answers0