Tutorial

Local Functions in MATLAB

Local functions are only available to other functions in the same file.

Description

Local functions provide a way to modularize a program, which makes the code easier to understand and maintain. Instead of writing a long function to resolve a problem:

  1. Divide the problem in tasks
  2. Implement each task as a local function
  3. Orchestrate the local functions from the main function

Keep in mind that local functions can only be accessed from other functions in the same file, so if the logic of a local function is required outside the current file, it must be defined separately in its own file.

Local functions are often called “subfunctions”.

Definition

Local functions can appear in any order in the file, but after the main function. The main function is the first function in the file, and its name is equal to the name of the file.

Define local functions in the same way as any other function, but take into account that local functions have their own workspace. This means that a local function cannot access variables used by other functions, including the functions in the same file.

function main()
A = 1;
disp(['Variable A in ''main'' is: ', num2str(A)]);
local();
disp(['Variable A in ''main'' is: ', num2str(A)]);
end
function local()
A = 2;
disp(['Variable A in ''local'' is: ', num2str(A)]);
end

In the preceding code, main is the main function and local is a local function. If you call main, the output evidences that although both functions use a variable named “A”, these variables are completely independent.

The file ‘main.m’ must be in the current directory or in the search path.
% Call the function 'main'
main();

Precedence

Local functions have precedence over functions in other files.

This fact means that when you call a function within a program file, MATLAB checks if the function is a local function in the current file, or a nested function. If no coincidence is found, then MATLAB searches the function in other files.

Nested functions have precedence over local functions.

Further reading

I recommend the following books to learn more on functions in MATLAB.

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    This book was the winner of a Textbook Excellence Award. The sixth chapter covers user-defined functions, scope of variables, debugging, and other aspects related to the development of MATLAB programs.

  2. MATLAB for Engineers (5th Edition)

    This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. Chapter 6 explains how to create functions, including local functions, anonymous functions, and nested functions.

I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series.

web(fullfile(docroot, 'matlab/matlab_prog/local-functions.html'));

Source code

I hope you have learned what local functions are in MATLAB. The source code developed in this tutorial is available at this page.

Share

Your friends may need this knowledge to complete a task. Share this tutorial.