Tutorial

Private Functions in MATLAB

Private functions are stored in a directory named “private”.

Description

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.

Definition

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

Example

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

Call 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();

But if you call private1 or private2 from anywhere outside the “toolbox” directory, MATLAB generates an error.

% Call the function 'private1'
private1();
% Call the function 'private2'
private2();

Precedence

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.

Private functions have lower priority than local functions and nested 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 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.

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

Source code

I hope you have learned what private 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.