Skip to Main Content Skip to Search
Product Documentation

Overloading Functions for Your Class

Overloading MATLAB Functions

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.

Using MATLAB Functions in Conversion Methods

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 That Modify Default Behavior provides a discussion of methods that you might typically implement for MATLAB classes.

Implementing MATLAB Operators

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.

Rules for Naming to Avoid Conflicts

The names of methods, properties, and events are scoped to the class. Therefore, you should adhere to the following rules to avoid naming conflicts:

  


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