Alternative solution to polymorphism and heterogeneous hierarchies
5 views (last 30 days)
Show older comments
Dear Matlab Support,
I would like to implement something simular to the following picutre:

With the following requirements:
- The lowest level (cATSConst, cATSMap) should be hold in one list
- The common attributes of cASAP2Base are filled by the constuctor of cATSBase (could be abstract and does not have any attributes)
Is there a possability to create a list in Matlab 2018 and derive from two classes?
I hope the short description is suffitioned otherwise please let me know.
Best regards
Martin
0 Comments
Answers (1)
Leepakshi
on 12 Feb 2025
Hello Martin,
To implement the class hierarchy shown in the diagram using MATLAB, you'll need to define several classes using the below concepts:
1. matlab.mixin.Heterogeneous: This is a built-in class that allows heterogeneous arrays. Refer this documentation https://in.mathworks.com/help/matlab/ref/matlab.mixin.heterogeneous-class.html
2. cSLBlockBase: Inherits from matlab.mixin.Heterogeneous.
3. cASAP2Base: Inherits from cSLBlockBase.
4. cATSBase: Inherits from cASAP2Base.
5. cASAP2Const and cASAP2Map: Inherit from cASAP2Base.
6. cATSConst and cATSMap1: Inherit from cATSBase.
This will create a heterogeneous array of objects, allowing you to hold different types of objects in a single list.
- You may need to add additional methods and properties as required by your application.
- The cATSBase class constructor is used to fill common attributes, as per your requirement.
- You can create a list of objects from cATSConst and `cATSMap1` using an array of cATSBase type.
Here's a basic implementation in MATLAB:
NOTE: Please create a new class in a new file as only one class definition is allowed in one file.
% cSLBlockBase.m
classdef cSLBlockBase < matlab.mixin.Heterogeneous
properties
Name
Path
end
end
% cASAP2Base.m
classdef cASAP2Base < cSLBlockBase
properties
MaskName
MaskValue
end
methods
function obj = cASAP2Base(name, path, maskName, maskValue)
obj.Name = name;
obj.Path = path;
obj.MaskName = maskName;
obj.MaskValue = maskValue;
end
function str = StringTrim(obj)
str = strtrim(obj.MaskName);
end
end
end
% cATSBase.m
classdef cATSBase < cASAP2Base
methods
function obj = cATSBase(name, path, maskName, maskValue)
obj = obj@cASAP2Base(name, path, maskName, maskValue);
% Read base ATS info
end
end
end
% cASAP2Const.m
classdef cASAP2Const < cASAP2Base
end
% cASAP2Map.m
classdef cASAP2Map < cASAP2Base
properties
Axis
end
end
% cATSConst.m
classdef cATSConst < cATSBase
properties
SLOBJ
GDEVersion
RangeMax
end
end
% cATSMap1.m
classdef cATSMap1 < cATSBase
properties
SLOBJ
GDEVersion
RangeMax
end
end
Example of Creating a List
list = [cATSConst(), cATSMap1()];
Hope this helped.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!