MATLAB Object-Oriented Programming

(Joyce) #1

Implicit Class Conversion


In this section...
“Class Conversion Mechanism” on page 10-16
“Concatenation” on page 10-16
“Subscripted Assignment” on page 10-17

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.

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, MATLAB passes the object to be converted 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.

Concatenation


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 like double.

  • If there is no defined dominance relationship between any two objects, then the left-
    most 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.

10 Object Arrays

Free download pdf