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


Concatenating Objects

cat, horzcat, and vertcat

Customize behavior when concatenation objects

See Subclasses of Built-In Types with Properties

Creating Empty Arrays


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

Displaying Objects



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


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

See Defining end Indexing for an Object


Determine the number of elements in an array

See Interactions with numel, subsref, and subsasgn


Determine the dimensions in an array


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 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.

numel, subsref, and subsasgn — Considerations for Overloading

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, 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.

Syntax for Overloading numel, subsref, and subsasgn

When resolving an indexed reference or assignment, MATLAB calls numel to determine the number of outputs that are expected from subsref or the number of inputs required by subsasgn.

To support {} indexing, you need to overload numel to accept a variable number of input index values in addition to the object array. Define the overloaded numel with this syntax:

function n = numel(A,varargin)

where varargin represent indices into array A.

MATLAB uses the value returned by numel to determine:

  • How many outputs to return from subsref in the case of indexed reference

  • How many inputs to pass to subsasgn in the case of indexed assignment

When overloading subsref, define the method to return multiple values for the indexed reference using varargout:

function [varargout] = subsref(A,S)

When overloading subsasgn, define the method to accept multiple values for the right hand side of the indexed assignment:

function A = subsagn(A,S,varargin)
Was this topic helpful?