Main Content

Private Functions

This topic explains the termprivate function, and shows how to create and use private functions.

Private functions are useful when you want to limit the scope of a function. You designate a function as private by storing it in a subfolder with the nameprivate. Then, the function is available only to functions and scripts in the folder immediately above theprivatesubfolder.

For example, within a folder that is on the MATLAB®search path, create a subfolder namedprivate. Do not addprivateto the path. Within theprivatefolder, create a function in a file namedfindme.m:

functionfindme% FINDME An example of a private function.disp('You found the private function.')

Change to the folder that contains theprivatefolder and create a file namedvisible.m.

functionvisible findme

Change your current folder to any location and call thevisiblefunction.

visible
You found the private function.

Although you cannot call the private function from the command line or from functions outside the parent of theprivatefolder, you can access its help:

helpprivate/findme
findme An example of a private function.

Private functions have precedence over standard functions, so MATLAB finds a private function namedtest.mbefore a nonprivate program file namedtest.m. This allows you to create an alternate version of a particular function while retaining the original in another folder.

Related Topics