Methods for MATLAB® Classes

Supporting Typical MATLAB® Behavior

There are some standard methods that enable user-defined objects to behave like built-in MATLAB® classes. The following table lists some of the methods you might implement for MATLAB classes.

Class Method

Description

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.

converters like double and char

Convert an object to a MATLAB built-in class

subsref and subsasgn

Enables you to create nonstandard indexed reference and indexed assignment

end

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

subsindex

Supports using an object in indexing expressions

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 also need to overload numel to restore proper behavior when you have overloaded size to do something other than returning the true size of an object.

Displaying Objects in the Command Window

MATLAB calls a method named disp whenever an object is referred to in a statement that is not terminated by a semicolon. For example, the following statement creates the variable a and calls the MATLAB disp method for class double. This method simply displays the value of a in the command line.

a = 5
a =
    5

All MATLAB classes inherit default disp and display methods.

You can define a disp method for your classes if you want MATLAB to display more useful information on the command line when referring to objects from your class. In many classes, disp can simply print the variable name, and then use the char converter method to print the contents of the variable. You need to define the char method to convert the object's data to a character string because MATLAB displays output as character strings.

You might also use sprintf or other data formatting function to implement the disp method for your class.

Examples of disp Methods

See the following sections for examples of disp methods:

Displaying TensileData Objects

The DocPolynom disp Method

The DocAsset Display Method

The DocPortfolio disp Method

Relationship Between disp and display

MATLAB invokes the built-in display function when:

MATLAB invokes the built-in disp function in the following cases:

The built-in display function prints the name of the variable being displayed, if an assignment is made, or otherwise uses ans as the variable name. display then calls disp to handle the actual display of the values.

If the variable being displayed is an object of a class that overloads disp as a class method, then MATLAB always calls the overloaded method. You should overload disp or disp and display to customize the display of objects. Overloading only display is not adequate to property implement a custom display for your class.

Converting to Another Class

It is sometimes useful to convert an object of one class to an object of another class. A converter method has the same name as the class it converts to, such as char or double.

Converters enable you to:

For example, suppose you have defined a polynomial class, but you have not overloaded any of the MATLAB functions that operate on the coefficients of polynomials, which are doubles. If you create a double method for the polynomial class, you can use it to call other functions that require inputs of type double.

You can use the double method to call other functions:

roots(double(p))

where p is a polynomial object, double is a method of the polynomial class, and roots is a standard MATLAB function whose input arguments are the coefficients of a polynomial.

Examples of Converter Methods

See the following sections for examples of converter methods:

  


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