MATLAB® scripts, including live scripts, can contain code to define functions. These functions are called local functions. Local functions are useful if you want to reuse code within a script. By adding local functions, you can avoid creating and managing separate function files. They are also useful for experimenting with functions, which can be added, modified, and deleted easily as needed. Functions in scripts are supported in R2016b or later.
Local functions are only visible within the file where they are defined, both to the script code and other local functions within the file. They are not visible to functions in other files, and cannot be called from the command line. They are equivalent to subroutines in other programming languages, and are sometimes called subfunctions.
To add local functions to a script, first, create the script. Go to the Home tab and select New > Script. For more information about creating scripts, see Create Scripts. You also can Create Live Scripts in the Live Editor.
After you create the script, add code to the script and save it. For example, add
this code and save it as a script called
mystats.m. This code
declares an array, determines the length of the array, and passes both values to the
mymedian. The local
mymedian calculate the
average and median of the input list and return the results.
Including functions in scripts requires MATLAB R2016b or later.
x = 1:10; n = length(x); avg = mymean(x,n); med = mymedian(x,n); function a = mymean(v,n) % MYMEAN Example of a local function. a = sum(v)/n; end function m = mymedian(v,n) % MYMEDIAN Another example of a local function. w = sort(v); if rem(n,2) == 1 m = w((n + 1)/2); else m = (w(n/2) + w(n/2 + 1))/2; end end
You can add local functions in any order, as long as they all appear after the
rest of the script code. Each function begins with its own function definition
statement, and ends with the
end keyword. The definition
statement is the first executable line of any function, for example,
function a = mymean(v,n). For more information about function
definition statements, including how to create them, see Create Functions in Files.
Although you cannot call a local function from the command line or from functions
in other files, you can access its help using the
Specify the names of both the script and the local function, separating them with a
mymean Example of a local function.
To run a script, including all local functions, click the Run (for scripts) or Run All (for live scripts) button, or type the saved script name in the Command Window. You can also run individual sections in a script by clicking the Run Section button.
Local functions in the current file have precedence over functions in other files. That is, when you call a function within a program file, MATLAB checks whether the function is a local function before looking for other functions. This allows you to create an alternate version of a particular function while retaining the original in another file.
Scripts create and access variables in the base workspace. Local functions, similar to other functions, have their own workspaces that are separate from the base workspace. Local functions cannot access variables in the workspace of other functions or in the base workspace, unless you pass them as arguments. For more information, see Base and Function Workspaces.
The Live Editor does not support running individual sections within local functions. For this reason, you cannot add section breaks within local functions in live scripts. When you add local functions to a live script, MATLAB automatically adds a section break before the first local function definition, and removes all section breaks after it. You can however run individual sections within the script code, even if they contain a call to one of the local functions in the file. For more information on section breaks and running live scripts, see Run Sections in Live Scripts.