Private functions resolve a problem that other types of functions cannot resolve: How to make a function visible to a limited set of functions in other files?
- Local functions are only visible to other functions in the same file
- Nested functions are only visible to a reduced set of functions in the same file
- Standard functions are visible to the whole world
The common use of private functions is in toolboxes, when you need to create functions for internal use in the toolbox. For example, a function that performs a validation required by various functions in the toolbox.
Define private functions in the same way as any other function, but place them in a directory named “private”. Each function in the “private” directory will be visible to:
- Other functions in the “private” directory
- Functions in the parent of the “private” directory
Create a directory named “toolbox” and add it to the search path. Inside “toolbox”, create a directory named “private” and leave it out of the search path.
Below is the code of three functions. Save
main inside “toolbox”, and the other functions inside “private”.
function main() private1(); end
function private1() disp('Hello! I am ''private1'''); private2(); end
function private2() disp('Hello! I am ''private2'''); end
main and note that it can access the functions in the “private” directory, although this directory is not in the search path.
% Call the function 'main' main();
Hello! I am 'private1' Hello! I am 'private2'
But if you call
private2 from anywhere outside the “toolbox” directory, MATLAB generates an error.
% Call the function 'private1' private1();
Undefined function or variable 'private1'.
% Call the function 'private2' private2();
Undefined function or variable 'private2'.
Private functions have precedence over standard functions.
This fact means that when you call a function within a program file, MATLAB checks if the function is a private function in the current context. 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 private functions, local 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 private functions are in MATLAB. The source code developed in this tutorial is available at this page.