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:
- Divide the problem in tasks
- Implement each task as a local function
- 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 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.
% Call the function 'main' main();
Variable A in 'main' is: 1 Variable A in 'local' is: 2 Variable A in 'main' is: 1
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.
I recommend the following books to learn more on functions in MATLAB.
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.
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.
- How to Define Functions in MATLAB
- How to Call a Function in MATLAB
- How to Add Help Documentation to a Function in MATLAB
- Local Functions in MATLAB
- Nested Functions in MATLAB
- Visibility of Nested Functions in MATLAB
- Private Functions in MATLAB
- Anonymous Functions in MATLAB
- Inside the Handle of Anonymous Functions in MATLAB
- Functions with Variable Input Arguments in MATLAB
- Functions with Variable Output Arguments in MATLAB
- How to Validate the Number of Input Arguments Passed to a Function in MATLAB
- How to Validate the Number of Output Arguments Passed to a Function in MATLAB
I hope you have learned what local functions are in MATLAB. The source code developed in this tutorial is available at this page.