How are objects converted from one type to another when collected together in an array in MATLAB 7.6 (R2008a)?
Date Last Modified: Friday, June 26, 2009
| Solution ID: | 1-6K5PAO | |
| Product: | MATLAB | |
| Reported in Release: | R2008a | |
| Platform: | All Platforms | |
| Operating System: | All OS |
Subject:
How are objects converted from one type to another when collected together in an array in MATLAB 7.6 (R2008a)?
Problem Description:
I have a handle class "SuperClass" and a class "SubClass" which derives from SuperClass, with the following definitions:
classdef SuperClass < handle
properties
prop1
prop2
end
methods
function obj = SuperClass(prop1,prop2)
obj.prop1 = prop1;
obj.prop2 = prop2;
end
end
end
classdef SubClass < SuperClass
I would like to aggregate objects of these classes into a single vector as follows:
properties
newprop
end
methods
function obj = SubClass(prop1,prop2,newprop)
obj = obj@SuperClass(prop1,prop2);
obj.newprop = newprop;
end
end
end
supobj = SuperClass(1,2);
Conceptually, this should be possible because subobj is an object of SuperClass. However, this gives me the following error:
subobj = SubClass(3,4,5);
vec(1) = supobj;
vec(2) = subobj;
??? The following error occurred converting from SubClass to SuperClass:
Input argument "prop2" is undefined.
Solution:When MATLAB sees a subscripted assignment to an existing variable, MATLAB checks the class of the right-hand-side against the class of the left-hand-side (LHS) variable. If different, it attempts to convert the right-hand-side (RHS) variable to the type of the LHS class. |
|
|
