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.

3 Basic Program Components<br />

Forcing a Built-In Call<br />

If you overload any of the <strong>MATLAB</strong> built-in functions to handle a specific data<br />

type, then <strong>MATLAB</strong> will always call the overloaded function on that type. If,<br />

for some reason, you need to call the built-in version, you can override the<br />

usual calling mechanism using a function called builtin. The expression<br />

builtin('reshape', arg1, arg2, ..., argN);<br />

forces a call to <strong>MATLAB</strong> built-in reshape, passing the arguments shown even<br />

though an overload exists for the data types in this argument list.<br />

Overloaded <strong>MATLAB</strong> Functions<br />

An overloaded function is an additional implementation of an existing<br />

function that has been designed specifically to handle a certain data type.<br />

Whenyoupassanargumentofthistypeinacalltothefunction,<strong>MATLAB</strong><br />

looks for the function implementation that handles that type and executes<br />

that function code.<br />

Each overloaded <strong>MATLAB</strong> function has an M-file on the <strong>MATLAB</strong> path. The<br />

M-files for a certain data type (or class) are placed in a directory named with<br />

an @ sign followed by the class name. For example, to overload the <strong>MATLAB</strong><br />

plot function to plot expressions of a class named polynom differently than<br />

other data types, you would create a directory called @polynom and store your<br />

own version of plot.m in that directory.<br />

You can add your own overloads to any function by creating a class directory<br />

for the data type you wish to support for that function, and creating an M-file<br />

that handles that type in a manner different from the default. See “Setting<br />

Up Class Directories” on page 9-6 and “Designing User Classes in <strong>MATLAB</strong>”<br />

on page 9-9.<br />

When you use the which command with the -all option, <strong>MATLAB</strong> returns<br />

all occurrences of the file you are looking for. This is an easy way to find<br />

functions that are overloaded:<br />

which -all set<br />

% Show all implementations for 'set'<br />

3-112

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

Saved successfully!

Ooh no, something went wrong!