Implicit Class Conversion - MATLAB & Simulink (original) (raw)

MATLABĀ® can implicitly convert classes in these situations:

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:

For example, A is an instance of ClassA, andB is an instance of ClassB. In the statementC = [A,B], if A is the dominant object, MATLAB attempts to convert B to the class ofA.

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:

If the call to the ClassA constructor fails to convertB to ClassA, MATLAB issues an error. If the conversion succeeds, MATLAB concatenates A with the convertedB.

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 arrayA 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:

If calling the constructor fails, MATLAB calls cast:

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 SubClassB < ClassB 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:

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 SubClassA < ClassA 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:

Note

Argument validation does not support implicit conversion of any built-in types to cell arrays, even if you provide your own conversion method.

See Also

Topics