| MATLAB® | ![]() |
| On this page… |
|---|
Supporting Typical MATLAB® Behavior Caution When Overloading MATLAB® Functions |
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 |
|---|---|
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. | |
Convert an object to a MATLAB built-in class | |
Enables you to create nonstandard indexed reference and indexed assignment | |
Supports end syntax in indexing expressions using an object; e.g., A(1:end) | |
Supports using an object in indexing expressions |
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.
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.
See the following sections for examples of disp methods:
Displaying TensileData Objects
MATLAB invokes the built-in display function when:
Code executes a MATLAB statement that is not terminated with a semicolon and that returns a value.
Code explicitly invokes the display function.
MATLAB invokes the built-in disp function in the following cases:
The built-in display function calls disp.
Code explicitly invokes disp.
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.
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:
Use methods defined for another class
Ensure that expressions involving objects of mixed class types execute properly
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.
See the following sections for examples of converter methods:
![]() | Overloading MATLAB® Functions | Implementing Operators for Your Class | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |