## Documentation Center |

On this page… |
---|

How to Customizing Class Behavior Which Methods Control Which Behaviors |

There are functions that MATLAB^{®} calls implicitly when you
perform certain actions with objects. For example, a statement like `[B(1);A(3)]` involves
indexed reference and vertical concatenation.

You can change how user-defined objects behave by defining methods that control specific behaviors. To change a behavior, implement the appropriate method with the name and signature of the MATLAB function.

The following table lists the methods to implement for your class and describes the behaviors that they control.

Class Method to Implement | Description |
---|---|

| |

cat, horzcat, and vertcat | Customize behavior when concatenation objects |

| |

Create empty arrays of the specified class. See Creating Empty Arrays | |

| |

Called when you enter Called when statements are not terminated
by semicolons. | |

| |

Convert an object to a MATLAB built-in class See The DocPolynom to Character Converter and The DocPolynom to Double Converter | |

| |

Enables you to create nonstandard indexed reference and indexed assignment | |

Supports | |

Determine the number of elements in an array | |

Determine the dimensions in an array | |

Support using an object in indexing expressions | |

| |

loadobj and saveobj | Customize behavior when loading and saving objects |

Overloading and overriding are terms that describe techniques for customizing class behavior. Here is how we use these terms in MATLAB.

Overloading means that there is more than one function or method
having the same name within the same scope. MATLAB dispatches
to a particular function or method based on the dominant argument.
For example, the `timeseries` class overloads the MATLAB `plot` function.
When you call `plot` with a `timeseries` object
as an input argument, MATLAB calls
the `timeseries` class method named `plot`.

Overriding means redefining a method inherited from a superclass. MATLAB dispatches to the most specific version of the method. That is, if the dominant argument is an instance of the subclass, then MATLAB calls the subclass method.

Use the `InferiorClasses` attribute to control
class precedence. See Class Attributes for
more information.

Many MATLAB functions depend on the behavior of other functions,
like `size` and `numel`. Therefore, you must be careful
to ensure that what is returned by an overloaded version of these
functions is a correct and accurate representation of the size of
an object array.

You might need to define a `numel` method
to compensate when your class defines a specialized version of `size`.

`subsref` uses the value returned by `numel` to
compute the number of expected output arguments returned by `subsref` from
subscripted reference (i.e., `nargout`).

Similarly, `subsasgn` uses `numel` to
compute the expected number of input arguments to be assigned using `subsasgn` (i.e., `nargin`).

MATLAB determines the value of `nargin` for
an overloaded `subsasgn` function from the value
returned by `numel`, plus two (one for the variable
to which you are making an assignment and one for the `struct` array
of subscripts).

If MATLAB produces errors when calling your class's overloaded `subsref` or `subsagn` methods
because `nargout` is wrong for `subsref` or `nargin` is
wrong for `subsasgn`, then you need to overload `numel` to
return a value that is consistent with your implementation of these
indexing functions.

See Understanding size and numel and Indexed Reference and Assignment for more
information on implementing `subsref` and `subsagn` methods.

Was this topic helpful?