| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
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. 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.
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 |
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. | |
Converting Objects to Other Classes | |
Convert an object to a MATLAB built-in class See The DocPolynom to Character Converter and The DocPolynom to Double Converter | |
Indexing Objects | |
Enables you to create nonstandard indexed reference and indexed assignment | |
Supports end syntax in indexing expressions using an object; e.g., A(1:end) | |
Determine the number of elements in an array See Interactions with numel and Overloaded subsref and subsasgn | |
Determine the dimensions in an array | |
Support using an object in indexing expressions | |
Saving and Loading Objects | |
| loadobj and saveobj | Customize behavior when loading and saving objects |
See Implementing Operators for Your Class for a list of functions that implement operators like +, >, ==, and so on.
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.
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 - 5for 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.
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.
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 subsasign, 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.
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.
![]() | Specializing Object Behavior | Defining Concatenation for Your Class | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |