Main Content

Add Functions to Scripts

Starting in R2016b, 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.

Create a Script with Local Functions

To create a script or live script with local functions, go to the Home tab and select New > Script or New > Live Script. Then, add code to the script. Add all local functions at end of the file, after the script code. Include at least one line of script code before the local functions. Each local function must begin with its own function definition statement and end with the end keyword. The functions can appear in any order.

For example, create a script called mystats.m.

edit mystats

In the file, include two local functions, mymean and mymedian. The script mystats declares an array, determines the length of the array, and then uses the local functions mymean and mymedian to calculate the average and median of the array.

x = 1:10;
n = length(x);
avg = mymean(x,n);
med = mymedian(x,n);

function a = mymean(v,n)
% MYMEAN Local function that calculates mean of array.

    a = sum(v)/n;
end

function m = mymedian(v,n)
% MYMEDIAN Local function that calculates median of array.

    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

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. This is because live scripts do not support individual sections within local functions.

Run Scripts with Local Functions

To run a script or live script that includes local functions, in the Editor or Live Editor tab, click the Run button. You also can type the saved script name in the Command Window.

To run an individual section, place the cursor inside the section and use the Run Section button (requires R2017b or later for .m files). In .mlx files, you can run sections only when they are before the local function definitions.

Restrictions for Local Functions and Variables

Local functions are only visible within the file where they are defined. They are not visible to functions in other files, and cannot be called from the Command Window.

Local functions in the current file have precedence over functions in other files. That is, when you call a function within a script, MATLAB checks whether the function is a local function before looking for the function in other locations. 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, like all 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.

Access Help for Local Functions

Although you cannot call a local function from the command line or from functions in other files, you can access its help using the help command. Specify the names of both the script and the local function, separating them with a > character.

For example:

help mystats>mymean
 mymean Local function that calculates mean of array.

Related Topics