Overload Functions in Class Definitions
Why Overload Functions
Classes can redefine MATLAB® functions by implementing methods having the same name. Overloading is useful when defining specialized types that you want to behave like existing MATLAB types. For example, you can implement relational operations, plotting functions, and other commonly used MATLAB functions to work with objects of your class.
You can also modify default behaviors by implementing specific functions that control these behaviors. For more information on functions that modify default behaviors, see Methods That Modify Default Behavior.
Implementing Overloaded MATLAB Functions
Class methods can provide implementations of MATLAB functions that operate only on instances of the class. This restriction is possible because MATLAB can always identify to which class an object belongs.
MATLAB uses the dominant argument to determine which version of a function to call. If the dominant argument is an object, then MATLAB calls the method defined by the object's class, if one exists.
To overload a MATLAB function:
Define a method with the same name as the function you want to overload.
Ensure that the method argument list accepts an object of the class, which MATLAB uses to determine which version to call.
Perform the necessary steps in the method to implement the function. For example, access the object properties to manipulate data.
Generally, the method that overloads a function produces results similar to the MATLAB function. However, there are no requirements regarding how you implement the overloading method. The overloading method does not need to match the signature of the overloaded function.
MATLAB does not support defining multiple methods with the same name but different signatures in the same class.
Overload the bar Function
It is convenient to overload commonly used functions to work with objects of your class. For example, suppose that a class defines a property that stores data that you often graph. The
MyData class overrides the
bar function and adds a title to the graph:
classdef MyData properties Data end methods function obj = MyData(d) if nargin > 0 obj.Data = d; end end function bar(obj) y = obj.Data; bar(y,'EdgeColor','r'); title('My Data Graph') end end end
bar method has the same name as the MATLAB
bar function. However, the
bar method requires a
MyData object as input. Because the method is specialized for
MyData objects, it can extract the data from the
Data property and create a specialized graph.
To use the
bar method, create an object:
y = rand(1,10); md = MyData(y);
Call the method using the object:
You can also use dot notation:
Implementing MATLAB Operators
Classes designed to implement new MATLAB data types typically define certain operators, such as addition, subtraction, or equality.
For example, standard MATLAB addition (
+) 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
p1 + p2
For information on overloading operators, see Operator Overloading.
Rules for Naming to Avoid Conflicts
The names of methods, properties, and events are scoped to the class. Therefore, 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 local function 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.