MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

The local functions mymean and mymedian calculate the average and median of the input
list. The main function mystats determines the length of the list n and passes it to the
local functions.


Although you cannot call a local function from the command line or from functions in
other files, you can access its help using the help function. Specify names of both the file
and the local function, separating them with a > character:


help mystats>mymean


mymean Example of a local function.


Local functions in the current file have precedence over functions in other files. That is,
when you call a function within a program file, MATLAB checks whether the function is a
local function before looking for other main functions. Therefore, you can create an
alternate version of a particular function while retaining the original in another file.


All functions, including local functions, have their own workspaces that are separate from
the base workspace. Local functions cannot access variables used by other functions
unless you pass them as arguments. In contrast, nested functions (functions completely
contained within another function) can access variables used by the functions that
contain them.


See Also


localfunctions


More About



  • “Nested Functions” on page 20-32

  • “Function Precedence Order” on page 20-43


See Also
Free download pdf