This topic explains the term private 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 name
private. Then, the function is available only to functions in the
folder immediately above the
private subfolder, or to scripts called
by the functions that reside in the parent folder.
For example, within a folder that is on the MATLAB® search path, create a subfolder named
private. Do not
private to the path. Within the
folder, create a function in a file named
function findme % FINDME An example of a private function. disp('You found the private function.')
Change to the folder that contains the
private folder and create a
function visible findme
Change your current folder to any location and call the
You found the private function.
Although you cannot call the private function from the command line or from functions
outside the parent of the
private folder, you can access its
findme An example of a private function.
Private functions have precedence over standard functions, so MATLAB finds a private function named
test.m before a
nonprivate program file named
test.m. This allows you to create an
alternate version of a particular function while retaining the original in another