functions and nested
functions make use of variable data that is stored outside
the body of the function itself. For example, the anonymous function
shown here uses two variables:
You pass the
X variable into the anonymous function
whenever you invoke the function. The value for
is taken from the currently active workspace:
K = 200; fAnon = @(X)K * X; fAnon([2.54 1.43 0.68 1.90 1.02 2.13]);
What would happen if you tried to invoke this function after
K from the workspace? Or if you saved
the anonymous function to a
.mat file and then
loaded it into an entirely separate computing environment where
The answer is that MATLAB® stores any values needed by an anonymous (or nested) function within the handle itself. It does this at the time you construct the handle. This does not include values from the argument list as these values get passed in whenever you call the function.
If you create an anonymous function at the MATLAB command window, that function has access to the workspace of your current MATLAB session. If you create the function inside of another function, then it has access to the outer function's workspace. Either way, if your anonymous function depends upon variables from an outside workspace, then MATLAB stores the variables and their values within the function handle at the time the handle is created.
This example puts a 3-by-5 matrix into the base workspace, and then creates a function handle to an anonymous function that requires access to the matrix.
A and anonymous function
A = magic(5); A(4:5,:) =  A = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 testAnon = @(x)x * A; % Anonymous function
Call the anonymous function via its handle, passing in a multiplier value. This multiplies all elements by 5.2:
testAnon(5.2) ans = 88.4000 124.8000 5.2000 41.6000 78.0000 119.6000 26.0000 36.4000 72.8000 83.2000 20.8000 31.2000 67.6000 104.0000 114.4000
A from the base workspace
and verify that this action has no effect on the output of the anonymous
clear A testAnon(5.2) ans = 88.4000 124.8000 5.2000 41.6000 78.0000 119.6000 26.0000 36.4000 72.8000 83.2000 20.8000 31.2000 67.6000 104.0000 114.4000
This works because the variable
A and its
value at the time the anonymous function is created is preserved within
the function handle that also provides access to the anonymous function.
See Variables in the Expression for
Nested functions are similar to anonymous functions in their ability to preserve workspace data needed by a function handle. In this case however, the workspace belongs to one of the functions inside of which the handle is being created. See Sharing Variables Between Parent and Nested Functions and Using Handles to Store Function Parameters for more information on this subject.
This example shows a function
returns a handle to nested function
The nested function uses two variables,
from the workspace of the outer function. Calling
a function handle to the nested function and also stores these two
variables within that handle so that they will be available whenever
the nested function is invoked:
function handle = getHandles(adjust) const = 16.3; handle = @getApproxVal_V4; function vOut = getApproxVal_V4(vectIn) vOut = ((vectIn+adjust)*const) + ((vectIn-adjust)*const); end end
getHandles function to obtain
a handle to the nested function:
adjustVal = 0.023; getApproxValue = getHandles(adjustVal); getApproxValue([0.67 -0.09 1.01 0.33 -0.14 -0.23]) ans = 21.8420 -2.9340 32.9260 10.7580 -4.5640 -7.4980
The documentation on Examining a Function Handle explains how to see which variables are stored within a particular function handle. Another helpful resource is Using Handles to Store Function Parameters.
If you save a function handle to a nested function and, at some later date, modify the function and then reload the handle, you may observe unexpected behavior from the restored handle. when you invoke the function from the reloaded handle.