Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Functions Provided By MATLAB

Overview

Many of the functions provided with the MATLAB software are implemented as M-files just like the M-files you create with MATLAB. Other MATLAB functions are precompiled executable programs called built-ins that run much more efficiently. Many MATLAB functions are also overloaded so that they handle different classes 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

dir([matlabroot '\toolbox\matlab\'])

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:

exist repmat
ans =
     2

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 did not 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:

which repmat
   D:\matlabR14\toolbox\matlab\elmat\repmat.m

Built-In Functions

Functions that are frequently used or that can take more time to execute are often implemented as executable files. These functions are called built-ins.

Unlike M-file functions, you cannot see the source code for built-ins. Although most built-in functions do have an M-file associated with them, this file is there mainly to supply the help documentation for the function. Take the reshape function, for example, and find it on the MATLAB path:

which reshape
   D:\matlabR14\toolbox\matlab\elmat\reshape.m

If you type this M-file out, you will see that it consists almost entirely of help text. At the bottom is a call to the built-in executable image.

Identifying Built-In Functions

As with M-file functions, you can identify which functions are built-ins using the exist function. This function identifies built-ins by returning the number 5:

exist reshape
ans =
     5

Forcing a Built-In Call

If you overload any of the MATLAB built-in functions to handle a specific class, then MATLAB will always call the overloaded function on that type. If, for some reason, you need to call the built-in version, you can override the usual calling mechanism using a function called builtin. The expression

builtin('reshape', arg1, arg2, ..., argN);

forces a call to MATLAB built-in reshape, passing the arguments shown, even though an overload exists for the class in this argument list.

Overloaded MATLAB Functions

An overloaded function is an additional implementation of an existing function that has been designed specifically to handle a certain class. When you pass an argument of this type in a call to the function, MATLAB looks for the function implementation that handles that type and executes that function code.

Each overloaded MATLAB function has an M-file on the MATLAB path. The M-files for a certain class are placed in a directory named with an @ sign followed by the class name. For example, to overload the MATLAB plot function to plot expressions of a class named polynom differently than other class, you would create a directory called @polynom and store your own version of plot.m in that directory.

You can add your own overloads to any function by creating a class directory for the class you wish to support for that function, and creating an M-file that handles that type in a manner different from the default. See Defining Classes — Syntax and Developing Classes — Typical Workflow.

When you use the which command with the -all option, MATLAB returns all occurrences of the file you are looking for. This is an easy way to find functions that are overloaded:

which -all set          % Show all implementations for 'set'

Internal Utility Functions

The MathWorks™ reserves the use of packages named internal for utility functions used by internal MATLAB code. Functions that belong to an internal package are intended for The MathWorks use only. Using functions that belong to an internal package is strongly discouraged. These functions are not guaranteed to work in a consistent manner from one release to the next. In fact, any of these functions and classes may be removed from the MATLAB software in any subsequent release without notice and without documentation in the product release notes.

Any function called with a syntax that begins with the package name internal is an internal function. For example,

internal.matlab.functionname

Any function on the MATLAB path that resides at any level under a directory named +internal is an internal function. For example,

matlab\toolbox\matlab\+internal\functionname
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS