Documentation Center

  • Trial Software
  • Product Updates

Preserving Data from the Workspace

Both anonymous 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: X and K. You pass the X variable into the anonymous function whenever you invoke the function. The value for K however 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 you cleared 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 K is not defined?

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.

Preserving Data with Anonymous Functions

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.

Create matrix A and anonymous function testAnon:

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:

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

Clear variable A from the base workspace and verify that this action has no effect on the output of the anonymous function:

clear A

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 more information.

Preserving Data with Nested Functions

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 getHandles that returns a handle to nested function getApproxVal_V4. The nested function uses two variables, const and adjust, from the workspace of the outer function. Calling getHandles creates 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);

Call the 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.

Loading a Saved Handle to a Nested Function

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.

Was this topic helpful?