23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Overloading Operators and Functions<br />

Overloading Operators and Functions<br />

In many cases, you may want to change the behavior of the <strong>MATLAB</strong> operators<br />

and functions for cases when the arguments are objects. You can accomplish<br />

this by overloading the relevant functions. Overloading enables a function<br />

to handle different types and numbers of input arguments and perform<br />

whatever operation is appropriate for the highest-precedence object. See<br />

“Object Precedence” on page 9-70 for more information on object precedence.<br />

This section covers<br />

• “Overloading Operators” on page 9-23<br />

• “Overloading Functions” on page 9-25<br />

Overloading Operators<br />

Each built-in <strong>MATLAB</strong> operator has an associated function name (e.g., the +<br />

operator has an associated plus.m function). You can overload any operator<br />

by creating an M-file with the appropriate name in the class directory. For<br />

example, if either p or q is an object of type classname, the expression<br />

p + q<br />

generates a call to a function @classname/plus.m, ifitexists.Ifp and q are<br />

both objects of different classes, then <strong>MATLAB</strong> applies the rules of precedence<br />

to determine which method to use.<br />

Examples of Overloaded Operators<br />

See the following sections for examples of overloaded operators:<br />

• “Overloading the + Operator” on page 9-32<br />

• “Overloading the - Operator” on page 9-33<br />

• “Overloading the * Operator” on page 9-33<br />

The following table lists the function names for most of the <strong>MATLAB</strong><br />

operators.<br />

9-23

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!