|On this page…|
You can implement MATLAB® operators (+, *, >, etc.) to work with objects of your class. Do this by defining the associated class methods.
Each operator has an associated function (e.g., the + operator has an associated plus.m function). You can implement any operator by creating a class method with the appropriate name. This method can perform whatever steps are appropriate for the operation being implemented.
See MATLAB Operators and Associated Functions for a complete list of operator functions.
User-defined classes have a higher precedence than built-in classes. For example, if q is an object of class double and p is a user-defined class, MyClass, both of these expressions:
q + p p + q
generate a call to the plus method in the MyClass, if it exists. Whether this method can add objects of class double and class MyClass depends on how you implement it.
When p and q are objects of different classes, MATLAB applies the rules of precedence to determine which method to use.
Object Precedence in Expressions Using Operators provides information on how MATLAB determines which method to call.
Defining Arithmetic Operators for DocPolynom provides examples of overloaded operators.
The following table lists the function names for MATLAB operators. Implementing operators to work with arrays, (scalar expansion, vectorized arithmetic operations, an so on), might also require modifying indexing and concatenation.
Method to Define
Right element-wise division
Left element-wise division
Matrix right division
Matrix left division
Less than or equal to
Greater than or equal to
Not equal to
Complex conjugate transpose