| Contents | Index |
| On this page… |
|---|
Many of the functions provided with the MATLAB software are implemented as program files just like the 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.
If you look in the subfolders of the toolbox\matlab folder, you can find the sources to many of the functions supplied with MATLAB. Locate your toolbox\matlab folder by typing
dir([matlabroot '\toolbox\matlab\'])
Any MATLAB functions that you write are just like any other functions coded with MATLAB. When one of these functions is called, MATLAB parses and executes each line of code in the file. It saves the parsed version of the function in memory, eliminating parsing time on any further calls to this function.
To find out if a function is implemented with a program 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 a file with a .m file extension, then exist returns the number 2. This example identifies the source for the repmat function as a program 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 is known to MATLAB and returns 2 only on program files.
One advantage of functions implemented as files is that you can look at the source code. This can 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 function, use which. For example,
which repmat
returns the full path for the program file:
D:\MATLAB\toolbox\matlab\elmat\repmat.m
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 MATLAB program file functions, you cannot see the source code for built-ins. Although most built-in functions do have a program file associated with them, this file is there mainly to supply the help documentation for the function. For example, view the program file for the reshape function:
type reshape.m
The file contains only help text.
As with program 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
If you overload any of the MATLAB built-in functions to handle a specific class, then MATLAB always calls 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 the builtin function. The expression
builtin('reshape', arg1, arg2, ..., argN);
forces a call to the MATLAB built-in function, reshape, passing the arguments shown even though an overload exists for the class in this argument list.
Note With the exception of overloading, you should not create a MATLAB program file that has the same name as a MATLAB built-in. Because built-in functions have a higher precedence than most other types of program files (with the exception of private and subfunctions), MATLAB does not recognize functions that share the same name with a built-in. |
An overloaded function is an additional implementation of an existing function that is 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 a file on the MATLAB path. The files for a certain class reside in a folder named with an @ sign followed by the class name. For example, if you need to plot expressions of class polynom in a manner that is unique to that class, you can overload the MATLAB plot function. To do this, create your own plotting function plot.m specifically for use with objects of the polynom class. Then, create a folder called @polynom, and store your own version of plot.m in that folder.
You can add your own overloads to any function. Just create a class folder for the class you want to support for that function, and create a file that handles the 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'
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 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 could 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 folder named +internal is an internal function. For example,
matlab\toolbox\matlab\+internal\functionname
![]() | Calling Functions | Types of Functions | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |