| MATLAB® | ![]() |
| On this page… |
|---|
Class methods can provide implementations of MATLAB functions that operate only on instances of the class. This is possible because MATLAB software can always identify which class an object belongs to. The dominant argument is used to determine which version of a function to call. If the argument is an object, then MATLAB calls the method defined by the object's class, if there is one.
In cases where a class defines a function with the same name as a global function, the class's implementation of the function is said to overload the original global implementation.
You might want to overload a number of MATLAB functions to work with an object of your class. Often, a simple solution to providing a full set of MATLAB functionality for a class involves creating methods that convert the data contained in your object to a type that existing MATLAB functions can use.
For example, suppose you define a class to represent polynomials that can have only single precision coefficients. You want a roots method to work on objects of your new class, but want to use the existing MATLAB roots function, which accepts a row vector of doubles that are the coefficients of a polynomial, ordered in descending powers.
The following method accesses a class property, coefficients, that contains the polynomial's coefficients, converts them to type double, and then passes the vector of doubles to the built–in version of the roots function.
methods function rts = roots(polyobject) % Extract data for MATLAB version of roots function coef = double(polyobject.coefficients); rts = roots(coef); end end
Overloading MATLAB Functions for the DocPolynom Class provides examples.
Methods for MATLAB Classes provides a discussion of methods that you might typically implement for MATLAB classes.
Classes designed to implement new MATLAB data types typically overload certain operators, such as addition, indexed assignment, and so on.
For example, the addition + (plus) function cannot add two polynomials because this operation is not defined by simple addition. However, a polynomial class can define its own plus method that the MATLAB language calls to perform addition of polynomial objects when you use the + symbol:
p1 + p2
Implementing Operators for Your Class provides information on methods to overload.
Defining Arithmetic Operators for DocPolynom provides examples.
The names of methods, properties, and events are scoped to the class. Therefore, you should adhere to the following rules to avoid naming conflicts:
You can reuse names that you have used in unrelated classes.
You can reuse names in subclasses if the member does not have public or protected access. These names then refer to entirely different methods, properties, and events without affecting the superclass definitions
Within a class, all names exist in the same name space and must be unique. A class cannot define two methods with the same name and a class cannot define a subfunction with the same name as a method.
The name of a static method is considered without its class prefix. Thus, a static method name without its class prefix cannot match the name of any other method.
![]() | Static Methods | Methods for MATLAB Classes | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |