Documentation Center

  • Trial Software
  • Product Updates

Methods That Modify Default Behavior

How to Customizing Class Behavior

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.

Which Methods Control Which Behaviors

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

Class Method to Implement

Description

Concatenating Objects

cat, horzcat, and vertcat

Customize behavior when concatenation objects

See Subclasses of Built-In Types with Properties

Creating Empty Arrays

empty

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

Displaying Objects

disp

display

Called when you enter disp(obj) on the command line

Called when statements are not terminated by semicolons. disp is often used to implement display methods.

See Overload the disp Function

Converting Objects to Other Classes

converters like double and char

Convert an object to a MATLAB built-in class

See The DocPolynom to Character Converter and The DocPolynom to Double Converter

Indexing Objects

subsref and subsasgn

Enables you to create nonstandard indexed reference and indexed assignment

See Indexed Reference and Assignment

end

Supports end syntax in indexing expressions using an object; e.g., A(1:end)

See Defining end Indexing for an Object

numel

Determine the number of elements in an array

See Interactions with numel and subsref and subsasgn

size

Determine the dimensions in an array

subsindex

Support using an object in indexing expressions

See Using Objects as Indices

Saving and Loading Objects

loadobj and saveobj

Customize behavior when loading and saving objects

See Control Save and Load

Overloading and Overriding Functions and Methods

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

Overloading

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

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.

Caution When Customizing Classes

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.

Interactions with numel and subsref and subsasgn

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?