Main Content

Object Converters

Why Implement Converters

You can convert an object of one class to an object of another class. A converter method has the same name as the class it converts to, such as char or double. Think of a converter method as an overloaded constructor method of another class. The converter takes an instance of its own class and returns an object of a different class.

Converters enable you to:

  • Use methods defined for another class

  • Ensure that expressions involving objects of mixed class types execute properly

  • Control how instances are interpreted in other contexts

Suppose that you define a polynomial class. If you create a double method for the polynomial class, you can use it to call other functions that require inputs of type double.

p = polynomial(...);
dp = double(p);
roots(dp)

p is a polynomial object, double is a method of the polynomial class, and roots is a standard MATLAB® function whose input arguments are the coefficients of a polynomial.

Converters for Namespace Classes

Classes defined in namespaces can have names that are a dot-separated list of names. The last name is a class and preceding names are namespaces. Name the conversion methods using the namespace qualifiers in the method names. For example, a conversion method to convert objects of MyClass to objects of the nsp.NspClass class uses this method name:

classdef MyClass
   ...
   methods
      function objNspClass = nspname.NspClass(objMyclass)
         ...
      end
   end
end

You cannot define a converter method that uses dots in the name in a separate file. Define namespace-class converters in the classdef file.

Converters and Subscripted Assignment

When you make a subscripted assignment statement like:

A(1) = myobj;

MATLAB compares the class of the Right-Side variable to the class of the Left-Side variable. If the classes are different, MATLAB attempts to convert the Right-Side variable to the class of the Left-Side variable. To do this conversion, MATLAB first searches for a method of the Right-Side class that has the same name as the Left-Side class. Such a method is a converter method, which is similar to a typecast operation in other languages.

If the Right-Side class does not define a method to convert from the Right-Side class to the Left-Side class, MATLAB calls the Left-Side class constructor. passing it the Right-Side variable.

For example, suppose that you make the following assignments:

A(1) = objA; 
A(2) = objB;

MATLAB attempts to call a method of ClassB named ClassA. If no such converter method exists, MATLAB software calls the ClassA constructor, passing objB as an argument. If the ClassA constructor cannot accept objB as an argument, then MATLAB returns an error.

Use cell arrays to store objects of different classes.

Converter for Heterogeneous Arrays

To support the formation of heterogeneous arrays using objects that are not part of the heterogeneous hierarchy, implement a convertObject method in the root superclass. The convertObject method must convert the nonmember object to a valid member of the heterogeneous hierarchy.

For details on implementing the convertObject method, see matlab.mixin.Heterogeneous.

Related Topics