Skip to Main Content Skip to Search
Product Documentation

Methods That Modify Default Behavior

How to Modify 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. These functions enable user-defined objects to behave like instances of MATLAB built-in classes.

You can change how user-defined objects behave by overloading the function that controls the particular behavior. To change a behavior, implement the appropriate method with the same name and signature as the MATLAB function. If an overloaded method exists, MATLAB calls this method whenever you perform that action on an object of the class.

Which Methods Control Which Behaviors

The following table lists MATLAB functions and describes the behaviors that they control. Your class can overload these functions as class methods if you want to specialize the behaviors described.

Class Method to Implement

Description

Concatenating Objects

cat, horzcat, and vertcat

Customize behavior when concatenation objects

See Example — Adding Properties to a Built-In Subclass

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 Displaying Objects in the Command Window

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 Overloaded 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 Saving and Loading Objects

See Implementing Operators for Your Class for a list of functions that implement operators like +, >, ==, and so on.

Overloading and Overriding Functions and Methods

Overloading and overriding are ways to customize the behaviors of existing functions and methods. Both involve redefining how an existing function or method works by creating another one that MATLAB calls instead, under certain conditions. Which approach you use in any situation depends on the circumstances.

Here is how we use these terms in MATLAB. See Class Precedence for information on how MATLAB determines argument dominance.

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

When to Overload MATLAB Functions

You do not need to overload the MATLAB functions if you do not want to modify the behavior of your class. However, you might need to overload certain functions when your class defines specialized behaviors that differ from the default.

Example of Modified Behavior

For example, MATLAB defines indexed reference of an array:

p(3)

as a reference to the third element in the array p.

However, suppose you define a class to represent polynomials and you want an indexed reference like:

polyobj(3)

to cause an evaluation of the scalar polynomial object with the value of the independent variable equal to the index value, 3. You overload the subsref function for the polynomial class to accomplish this.

See The DocPolynom subsref Method for an example.

Select the appropriate function from the preceding table to change the behavior indicated. For example, MATLAB displays certain information about objects when you use the disp function or when you enter a statement that returns an object and is not terminated by a semicolon. Suppose you want your polynomial class to display the MATLAB expression for the polynomial represented by the object, instead of the default behavior. The display might look like this:

>> p

p = 

     x^3 - 2*x - 5

for a polynomial with the coefficients [1 0 2 -5].

You can implement this specialized behavior by overloading the disp and char methods. See The DocPolynom disp Method for an example that shows how to implement this change.

Caution When Overloading MATLAB Functions

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

You might need to overload numel to restore proper behavior when you have overloaded size to perform an action that is appropriate for your class design.

Interactions with numel and Overloaded subsref and subsasgn

You must ensure that the value returned by numel is appropriate for your class design when you overload subsref and subsasgn. subsref uses numel to compute the number of expected output arguments returned from subsref (i.e., nargout). Similarly, subsasgn uses numel to compute the expected number of input arguments to be assigned using subsasgn (i.e., nargin).

The value of nargin for an overloaded subsasgn function is determined by 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 the MATLAB runtime 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.

Ensuring MATLAB Calls Your Overloaded Method

When invoking an overloaded method, be sure that the object passed is the dominant type in the argument list. To ensure that MATLAB dispatches to the correct function, use dot notation for method invocation:

obj.methodName(args)

See Determining Which Method Is Invoked for more information.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS