MATLAB Object-Oriented Programming

(Joyce) #1

NoteMATLAB does not convert objects to a common superclass unless those objects are
part of a heterogeneous hierarchy. For more information, see “Designing Heterogeneous
Class Hierarchies” on page 10-24.


Concatenating Objects


Concatenation combines objects into arrays:


ary = [obj1,obj2,obj3,...,objn];


The size of ary is 1-by-n.


ary = [obj1;obj2;obj3;...;objn];


The size of ary is n-by-1.


The class of the arrays is the same as the class of the objects being concatenated.
Concatenating objects of different classes is possible if MATLAB can convert objects to
the dominant class. MATLAB attempts to convert unlike objects by:



  • Calling the inferior object converter method, if one exists.

  • Passing an inferior object to the dominant class constructor to create an object of the
    dominant class.


If conversion of the inferior object is successful, MATLAB returns an array that is of the
dominant class. If conversion is not possible, MATLAB returns an error.


Calling the Dominant-Class Constructor


MATLAB calls the dominant class constructor to convert an object of an inferior class to
the dominant class. MATLAB passes the inferior object to the constructor as an argument.
If the class design enables the dominant class constructor to accept objects of inferior
classes as input arguments, then concatenation is possible without implementing a
separate converter method.


If the constructor simply assigns this argument to a property, the result is an object of the
dominant class with an object of an inferior class stored in a property. If this assignment
is not a desired result, then ensure that class constructors include adequate error
checking.


Concatenating Objects of Different Classes............. 10-

Free download pdf