How to cast a subclass object to its superclass type..?

18 Ansichten (letzte 30 Tage)
Bradley Stiritz
Bradley Stiritz am 10 Sep. 2011
Hi everyone,
The MATLAB documentation states:
"MATLAB does not allow you to create arrays containing a mix of superclass and subclass objects because an array can be of only one class."
The MATLAB documentation for function cast() states:
"B = cast(A, newclass) converts A to class newclass, where newclass is the name of a built-in data type compatible with A."
Since we can't use the cast() function to convert a subclass to its superclass, how else can we perform the necessary conversion, in this or any general context..?
For example:
classdef MySuperClass
...
end
classdef MySubClass < MySuperClass
...
end
% Create subclass object:
mySubClassObj = MySubClass(..);
% Try to cast to superclass:
mySubClassObj = cast(mySubClassObj,'MySuperClass');
Error using cast
Unsupported class for conversion.
Any guidance / suggestions appreciated,
Thanks, Brad

Akzeptierte Antwort

Daniel Shub
Daniel Shub am 14 Sep. 2011
First, since r2011a MATLAB supports "Heterogeneous Arrays":
You can also essentially do what cast does by defining your own conversion method:
  1 Kommentar
Bradley Stiritz
Bradley Stiritz am 14 Sep. 2011
Hi Daniel, thanks very much for digging out these DOC pages for me, very appreciated :)
brad

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Subclass Definition finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by