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 file. 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 the Live Editor
does 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.
18 Scripts
18-12
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 file. 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 the Live Editor
does 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.
18 Scripts
18-12
