Implicit Class Conversion

Class Conversion Mechanism

When you create or modify object arrays using concatenation or subscripted assignment, MATLAB® attempts to convert unlike types to conform to the class of the array. MATLAB performs this conversion implicitly.

To perform the conversion, MATLAB attempts to call a converter method defined by the class to be converted. A converter method has the same name as the destination class. For example, if a class defines a method named double, this method converts an object of the class to an object of class double.

If no converter exists in the source object's class, then this call resolves to a call to the constructor of the destination class.

Both concatenation and subscripted assignment can cause MATLAB to apply this class conversion mechanism. The conversion can be successful or can result in an error if the conversion is not possible.


In concatenation operations, the dominant object determines the class of the resulting array. MATLAB determines the dominant object as follows:

  • User-defined classes are dominant over built-in classes such as double.

  • If there is no defined dominance relationship between any two objects, then the leftmost object dominates

For example, in the statement C = [A,B], if A is the dominant object, MATLAB attempts to convert B to the class of A.

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 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 first looks for a converter method defined by the class of the source object B. This converter method must have the name ClassA in this case. The subscripted assignment is effectively a call to the converter defined by ClassB:

A(2) = ClassA(B) % Call method of ClassB

If no converter method exists, this call resolves to a call to the destination class constructor:

A(2) = ClassA(B) % Call ClassA constructor

Related Topics