Main Content

Operator Overloading

Why Overload Operators

By implementing operators that are appropriate for your class, you can integrate objects of your class into the MATLAB® language. For example, objects that contain numeric data can define arithmetic operations like +, *, - so that you can use these objects in arithmetic expressions. By implementing relational operators, you can use objects in conditional statements, like switch and if statements.

How to Define Operators

You can implement MATLAB operators to work with objects of your class. To implement operators, define 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.

For a list of operators and associated function names, see MATLAB Operators and Associated Functions.

Object Precedence in Operations

User-defined classes have a higher precedence than built-in classes. For example, suppose q is an object of class double and p is a user-defined class. Both of these expressions generate a call to the plus method in the user-define class, if it exists:

q + p 
p + q

Whether this method can add objects of class double and the user-defined class depends on how you implement the method.

When p and q are objects of different classes, MATLAB applies the rules of precedence to determine which method to use.

For more information on how MATLAB determines which method to call, see Method Invocation.

Operator Precedence

Overloaded operators retain the original MATLAB precedence for the operator. For information on operator precedence, see Operator Precedence.

Sample Implementation — Addable Objects

The Adder class implements addition for objects of this class by defining a plus method. Adder defines addition of objects as the addition of the NumericData property values. The plus method constructs and returns an Adder object whose NumericData property value is the result of the addition.

The Adder class also implements the less than operator (<) by defining a lt method. The lt method returns a logical value after comparing the values in each object NumericData property.

classdef Adder
   properties
      NumericData
   end
   methods
      function obj = Adder(val)
         obj.NumericData = val;
      end
      function r = plus(obj1,obj2)
         a = double(obj1);
         b = double(obj2);
         r = Adder(a + b);
      end
      function d = double(obj)
         d = obj.NumericData;
      end
      function tf = lt(obj1,obj2)
         if obj1.NumericData < obj2.NumericData
            tf = true;
         else
            tf = false;
         end
      end
   end
end

Using a double converter enables you to add numeric values to Adder objects and to perform addition on objects of the class.

a = Adder(1:10)
a = 

  Adder with properties:

    NumericData: [1 2 3 4 5 6 7 8 9 10]

Add two objects:

a + a
ans = 

  Adder with properties:

    NumericData: [2 4 6 8 10 12 14 16 18 20]

Add an object with any value that can be cast to double:

b = uint8(255) + a
b = 

  Adder with properties:

    NumericData: [256 257 258 259 260 261 262 263 264 265]

Compare objects a and b using the < operator:

a < b
ans =

     1

Ensure that your class provides any error checking required to implement your class design.

MATLAB Operators and Associated Functions

The following table lists the function names for MATLAB operators. Implementing operators to work with arrays (scalar expansion, vectorized arithmetic operations, and so on), can also require modifying indexing and concatenation. Use the links in this table to find specific information on each function.

Operation

Method to Define

Description

a + b

plus(a,b)

Binary addition

a - b

minus(a,b)

Binary subtraction

-a

uminus(a)

Unary minus

+a

uplus(a)

Unary plus

a.*b

times(a,b)

Element-wise multiplication

a*b

mtimes(a,b)

Matrix multiplication

a./b

rdivide(a,b)

Right element-wise division

a.\b

ldivide(a,b)

Left element-wise division

a/b

mrdivide(a,b)

Matrix right division

a\b

mldivide(a,b)

Matrix left division

a.^b

power(a,b)

Element-wise power

a^b

mpower(a,b)

Matrix power

a < b

lt(a,b)

Less than

a > b

gt(a,b)

Greater than

a <= b

le(a,b)

Less than or equal to

a >= b

ge(a,b)

Greater than or equal to

a ~= b

ne(a,b)

Not equal to

a == b

eq(a,b)

Equality

a & b

and(a,b)

Logical AND

a | b

or(a,b)

Logical OR

~a

not(a)

Logical NOT

a:d:b

a:b

colon(a,d,b)

colon(a,b)

Colon operator

a'

ctranspose(a)

Complex conjugate transpose

a.'

transpose(a)

Matrix transpose

[a b]

horzcat(a,b,...)

Horizontal concatenation

[a; b]

vertcat(a,b,...)

Vertical concatenation

a(s1,s2,...sn)

subsref(a,s)

Subscripted reference

a(s1,...,sn) = b

subsasgn(a,s,b)

Subscripted assignment

b(a)

subsindex(a)

Subscript index

Related Topics