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.

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

The exist function also returns 2 for files that have a file type unknown to<br />

<strong>MATLAB</strong>. However, if you invoke exist on a <strong>MATLAB</strong> function name, the file<br />

type will be known to <strong>MATLAB</strong> and will return 2 only on M-files.<br />

Viewing the Source Code<br />

One advantage of functions implemented as M-files is that you can look<br />

at the source code. This may help when you need to understand why the<br />

function returns a value you didn’t expect, if you need to figure out how to<br />

code something in <strong>MATLAB</strong> that is already coded in a function, or perhaps to<br />

help you create a function that overloads one of the <strong>MATLAB</strong> functions.<br />

To find the source code for any <strong>MATLAB</strong> M-file function, use which:<br />

which repmat<br />

D:\matlabR14\toolbox\matlab\elmat\repmat.m<br />

Built-In Functions<br />

Functions that are frequently used or that can take more time to execute are<br />

often implemented as executable files. These functions are called built-ins.<br />

Unlike M-file functions, you cannot see the source code for built-ins. Although<br />

most built-in functions do have an M-file associated with them, this file is<br />

there mainly to supply the help documentation for the function. Take the<br />

reshape function, for example, and find it on the <strong>MATLAB</strong> path:<br />

which reshape<br />

D:\matlabR14\toolbox\matlab\elmat\reshape.m<br />

If you type this M-file out, you will see thatitconsistsalmostentirelyofhelp<br />

text. At the bottom is a call to the built-in executable image.<br />

Identifying Built-In Functions<br />

As with M-file functions, you can identify which functions are built-ins using<br />

the exist function. This function identifies built-ins by returning the number<br />

5:<br />

exist reshape<br />

ans =<br />

5<br />

3-111

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

Saved successfully!

Ooh no, something went wrong!