MATLAB Object-Oriented Programming

(Joyce) #1

Concatenation Methods


In this section...
“Default Concatenation” on page 17-11
“Methods to Overload” on page 17-11

Default Concatenation


You can concatenate objects into arrays. For example, suppose that you have three
instances of the class MyClass, obj1, obj2, obj3. You can form arrays of these objects
using brackets. Horizontal concatenation calls horzcat:

HorArray = [obj1,obj2,obj3];

HorArray is a 1-by-3 array of class MyClass. You can concatenate the objects along the
vertical dimension, which calls vertcat:

VertArray = [obj1;obj2;obj3]

VertArray is a 3-by-1 array of class MyClass. To concatenate arrays along different
dimensions, use the cat function. For example:

ndArray = cat(3,HorArray,HorArray);

ndArray is a 1-by-3-by-2 array.

Methods to Overload


Overload horzcat, vertcat, and cat to produce specialized behaviors in your class.
Overload both horzcat and vertcat whenever you want to modify object concatenation
because MATLAB uses both functions for any concatenation operation.

See Also


Related Examples



  • “Subclasses of Built-In Types with Properties” on page 12-68


Concatenation Methods
Free download pdf