storing multiple objects derived from two different classes in one array

11 views (last 30 days)
I have two objects, lets say user_1 and user_2 that do not belong to the same class, I want to know if it is possible to store them in the same array.
  2 Comments
Afaf Arfaoui
Afaf Arfaoui on 4 Mar 2019
Actually I have defined one class called "User" after that I realized that I will need to define 2 types of users using two classes, lets say UserType1 and UserType2.
The problem is that my code was working using an array of users of the old class "User" only. When I defined two seperate classes I wasn't able to store the users of different classes in the same array and all my code was about this array.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 4 Mar 2019
Edited: Matt J on 4 Mar 2019
I want to know if it is possible to store them in the same array.
Yes, if that array is a cell array. Cell arrays can store any mixture of types,
array={user_1, user_2}
  3 Comments
Afaf Arfaoui
Afaf Arfaoui on 4 Mar 2019
Okay I see. I've tried the suggestion of Steven Lord in the comment below and it's working using a simple array. The class of the array is the super class 'User' but it contains objects from different classes that inherit from the super class. Thanks for your help!

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 4 Mar 2019
If those objects are in the same class hierarchy, you could make the hierarchy heterogeneous.

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!