Implicit Class Conversion
MATLAB® can implicitly convert classes in these situations:
Creation or modification of object arrays using concatenation
Creation or modification of object arrays using subscripted assignment
Property validation
Argument validation in function and method calls
To perform the conversion, MATLAB attempts to use a converter method, the constructor of the destination class,
or the cast
function, depending on the context of the
conversion.
Concatenation
In concatenation operations, the dominant object determines the class of the resulting array. MATLAB determines the dominant object according to these rules:
User-defined classes are dominant over built-in classes such as
double
.If there is no defined dominance relationship between any two classes, then the leftmost object in the concatenation statement is dominant. For more information on class dominance, see Method Invocation.
For example, A
is an instance of ClassA
, and
B
is an instance of ClassB
. In the statement
C = [A,B]
, if A
is the dominant object,
MATLAB attempts to convert B
to the class of
A
.
MATLAB first tries to call a converter method. If no converter method is found,
it calls the constructor for ClassA
. The concatenation statement is
equivalent to:
C = [A,ClassA(B)]
If the call to the ClassA
constructor fails to convert
B
to ClassA
, MATLAB issues an error. If the conversion succeeds, MATLAB concatenates A
with the converted
B
.
Subscripted Assignment
In subscripted assignment, the left side of the assignment statement defines the class of the array. If you assign array elements when the right side is a different class than the left side, MATLAB attempts to convert the right side to the class of the left side.
For example, assigning an object of ClassB
to an element of array
A
requires conversion.
A = ClassA; B = ClassB; A(2) = B;
MATLAB attempts to perform the conversion by first looking for a converter
method. If no converter method is found, it calls the ClassA
constructor.
The assignment is then equivalent to:
A(2) = ClassA(B)
If calling the constructor fails, MATLAB calls cast
:
A(2) = cast(B,"like",A)
If the conversion still fails after these steps, MATLAB issues an error. If the conversion succeeds, MATLAB assigns the converted value to A(2)
.
Property Validation
When you assign a value to a property that specifies a class restriction as part of
its validation, MATLAB uses the built-in function isa
to check the
relationship between the value and the class. If the value is not the specified class or
one of its subclasses, MATLAB attempts to convert the value to the specified class.
To demonstrate the conversion process, refer to these class definitions.
classdef ClassA properties Prop ClassB end end
classdef ClassB end
classdef SubClassB < ClassB end
classdef ClassC end
This script shows when MATLAB attempts conversions during property assignments.
A = ClassA; B = ClassB; SB = SubClassB; C = ClassC; A.Prop = B; % no conversion A.Prop = SB; % no conversion A.Prop = C; % conversion required
In this script:
A.Prop = B
does not require conversion becauseB
belongs toClassB
, which satisfies the property validation forProp
defined inClassA
.A.Prop = SB
does not require conversion becauseSB
belongs toSubClassB
, which is a subclass ofClassB
.A.Prop = C
requires conversion becauseC
does not belong toClassB
or its subclassSubClassB
.MATLAB attempts to convert
C
toClassB
or its subclassSubClassB
by first callingClassB(C)
. This call can invoke a converter method calledClassB
(defined byClassC
) or theClassB
constructor. If callingClassB(C)
does not yield an instance ofClassB
orSubClassB
, MATLAB attempts to cast the result ofClassB(C)
toClassB
as a final step.If these steps fail to convert
C
toClassB
orSubClassB
, MATLAB issues an error. If the conversion succeeds, MATLAB writes the converted value toA.Prop
.
Note
Property class validation does not support implicit conversion of any built-in types to cell arrays, even if you provide your own conversion method.
Function and Method Argument Validation
When you assign a value to a function or method argument that specifies a class
restriction as part of its validation, MATLAB uses the built-in function isa
to check the
relationship between the value and the class. If the value is not the specified class or
one of its subclasses, MATLAB attempts to convert the value to the specified class.
To demonstrate the conversion process, refer to these class and function definitions.
classdef ClassA end
classdef SubClassA < ClassA end
classdef ClassB end
function test(x) arguments x ClassA end end
This script shows when MATLAB attempts conversions during argument validation.
A = ClassA; SA = SubClassA; B = ClassB; test(A); % no conversion test(SA); % no conversion test(B); % conversion required
In this script:
test(A)
does not require conversion becauseA
belongs toClassA
, which satisfies the argument validation forx
defined intest
.test(SA)
does not require conversion becauseSA
belongs toSubClassA
, which is a subclass ofClassA
.test(B)
requires conversion becauseB
does not belong toClassA
or its subclassSubClassA
.MATLAB attempts to convert
B
toClassA
or its subclassSubClassA
by first callingClassA(B)
. This call can invoke a converter method calledClassA
(defined byClassB
) or theClassA
constructor. If callingClassA(B)
does not yield an instance ofClassA
orSubClassA
, MATLAB attempts to cast the result ofClassA(B)
toClassA
as a final step.If these steps fail to convert
B
toClassA
orSubClassA
, MATLAB issues an error. If the conversion succeeds, MATLAB uses the converted value for the function call.
Note
Argument validation does not support implicit conversion of any built-in types to cell arrays, even if you provide your own conversion method.