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 theprivate
subfolder.
For example, within a folder that is on the MATLAB®search path, create a subfolder namedprivate
. Do not addprivate
to the path. Within theprivate
folder, 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 theprivate
folder and create a file namedvisible.m
.
functionvisible findme
Change your current folder to any location and call thevisible
function.
visible
You found the private function.
Although you cannot call the private function from the command line or from functions outside the parent of theprivate
folder, 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.m
before 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.