Problem to use array of subclasses derived from superclass
23 views (last 30 days)
Show older comments
Hi all,
I met a problem of using the array of subclass which are derived from the same superclass.
I have several subclasses like: SubClass1, SubClass2, ... ..., SubClassN which are defined as:
SubClass1 < SupperClass, .... ..., SubClassN < SuperClass.
SuperClass is defined as:
classdef SuperClass < handle
Then I create an array of objects as:
ClassCall = [SubClass1; SubClass2; ... ... SubClassN]
for i = 1:N ; ObjectArray(i) = eval[ ClassCall(i) '(parameter list)']; end
This way I can create an array of objects based on the ClassCall. The code works fine if ClassCall consists of the same subclass like:
ClassCall = [SubClass1; SubClass1; ... ...; SubClass1]
However, the code will not work if the ClassCall consists of different subclass, it always gives the error message:
The following error occurred converting from SubClass1 to SubClass2 ...
I thought the problem may be due to the SuperClass definition, to accept hertergenous subclass, I changed the SuperClass definition to be:
classdef SuperClass < matlab.mixin.Heterogeneous
However, this definition will only create empty subclasses.
Any comments on this?
Thank you very much.
Jason
2 Comments
Daniel Hediger
on 13 Oct 2023
When creating an array of objects from different subclasses in MATLAB, it's essential to address potential type conversion issues. To work around this limitation, you can explicitly cast the objects to their specific subclasses after creating them. Here's a modified version of your code:
% Define the class names as strings
classNames = {'SubClass1', 'SubClass2', 'SubClass3', ...}; % Add all subclass names
% Create an empty array of objects
ObjectArray = SuperClass.empty(0, length(classNames));
% Create objects and cast them to their respective subclasses
for i = 1:length(classNames)
className = classNames{i};
ObjectArray(i) = feval(className, parameterList); % Create the object
ObjectArray(i) = cast(ObjectArray(i), className); % Explicitly cast to the subclass
end
In this code, we store class names as strings, create an empty array of SuperClass objects, and then explicitly cast each object to its respective subclass. This approach allows you to create an array of objects from different subclasses without encountering type conversion errors.
Answers (1)
Steven Lord
on 13 Oct 2023
Using a heterogenous array is the approach I'd use. I don't know if you're still reading this, but could you show what you mean by "empty subclasses" in the statement "However, this definition will only create empty subclasses."? A concrete example with say two subclasses and one base class would be useful.
Perhaps show us what you receive when you try running the example using the classes in the hierarchy designed on this documentation page? In the last code block on that page, what do you see when you create and display assetArray?
0 Comments
See Also
Categories
Find more on Subclass Definition 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!