Programming |
MATLAB Functions
Many of the functions provided with MATLAB are implemented as M-files just like the M-files that you will create with MATLAB. Other MATLAB functions are precompiled executable programs called built-ins that run much more efficiently.
This section discusses both types of functions and also functions that are overloaded to handle different data types appropriately:
M-File Functions
If you look in the subdirectories of the toolbox\matlab
directory, you can find the M-file sources to many of the functions supplied with MATLAB. You can locate your toolbox\matlab
directory by typing
MATLAB functions with an M-file source are just like any other functions coded with MATLAB. When one of these M-file functions is called, MATLAB parses and executes each line of code in the M-file. It saves the parsed version of the function in memory, eliminating parsing time on any further calls to this function.
Identifying M-File Functions
To find out if a function is implemented with an M-file, use the exist
function. The exist
function searches for the name you enter on the MATLAB path and returns a number identifying the source. If the source is an M-file, then exist
returns the number 2
. This example identifies the source for the repmat
function as an M-file:
The exist
function also returns 2 for files that have a file type unknown to MATLAB. However, if you invoke exist
on a MATLAB function name, the file type will be known to MATLAB and will return 2 only on M-files.
Viewing the Source Code
One advantage of functions implemented as M-files is that you can look at the source code. This may help when you need to understand why the function returns a value you didn't expect, if you need to figure out how to code something in MATLAB that is already coded in a function, or perhaps to help you create a function that overloads one of the MATLAB functions.
To find the source code for any MATLAB M-file function, use which
:
Space Character | Built-In Functions |
© 1994-2005 The MathWorks, Inc.