Main Content

Methods That Modify Default Behavior

How to Customize 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 concatenating objects

See Subclasses of Built-In Types with Properties

Creating Empty Arrays

empty

Create empty arrays of the specified class. See Empty Arrays

Displaying Objects

disp

display

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

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

See Overloading the disp Function

See Custom Display Interface

Converting Objects to Other Classes

converters like double and char

Convert an object to a MATLAB built-in class

See The Character Converter and The Double Converter

Indexing Objects

subsref and subsasgn

Enables you to create nonstandard indexed reference and indexed assignment

See Object Array Indexing

end

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

See end as Object Index

numel

Determine the number of elements in an array

See Modify nargout and nargin for Indexing Methods

numArgumentsFromSubscript

Overload to specify the number of values to return from indexing expressions.

See Number of Arguments for subsref and subsasgn

size

Determine the dimensions of an array

See Use of size and numel with Classes

subsindex

Support using an object in indexing expressions

See Objects in Index Expressions

Saving and Loading Objects

loadobj and saveobj

Customize behavior when loading and saving objects

See Object Save and Load

Reshape and Rearrange

permute

Rearrange dimensions of N-D array

transpose

Transpose vector or matrix

ctranspose

Complex conjugate transpose

reshape

Reshape array

repmat

Replicate array along specified dimensions

Determine Size and Shape

isscalar

Determine if the input is a scalar

isvector

Determine if the input is a vector

ismatrix

Determine if the input is a matrix

isempty

Determine if the input is empty

Overload Functions and Override 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.

To call the nonoverloaded function, use the builtin function.

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 object of the subclass, then MATLAB calls the subclass method.

To control class dominance, use the InferiorClasses attribute.

Related Topics