The workspaces for nested and anonymous functions are static. This means that all variables used within the function must be present in the text of the code.
If you attempt to dynamically add a variable to the static workspace of an anonymous function, a nested function, or a function that contains a nested function, then MATLAB® issues an error of the form
Attempt to add variable to a static workspace.
One way to avoid dynamically adding a variable to static workspaces is to explicitly declare the variable in the code before dynamically assigning a value to that variable. Doing so will cause the variable name to be visible to MATLAB, so the name will be included in the fixed set of variables that make up the static workspace.
For example, suppose a script named
assigns a value to variable
X. A function that calls
and explicitly declares
X avoids the
dynamic adding error because
X is in the function workspace.
A common way to declare a variable is to initialize its value to an empty array:
function noerror nestedfx function nestedfx X = ; makeX end end
assigninto Assign New Variables In a Nested Function
assignin to assign new variables inside of a nested
functions will generate an error.
function staticWorkspaceErrors function nest % This will error since x is not declared outside of the eval eval("x=2"); end end
If possible, avoid these functions altogether. See Alternatives to the eval Function. If it is not possible to avoid them, then explicitly declare the variable within the parent function:
function noStaticWorkspaceErrors x = ; function nest % This will not error since 'x' is declared outside of the eval eval("x=2"); end end
Calling a MATLAB script that creates a variable inside of a nested function will
generate an error. In the example below, the script,
scriptThatIntroducesZ, contains code that assigns a value
to the variable
z. Since the code does not explicitly declare
z is being assigned an error will be thrown.
function staticWorkspaceErrors function nest % This will error since 'z' is not declared outside of this script scriptThatIntroducesZ end end
To avoid an error, declare the variable within the function before calling the script that assigns a value to it.
function noStaticWorkspaceErrors function nest % This will not error since 'z' is declared outside of the script z = ; scriptThatIntroducesZ end end
Alternatively, convert the script to a function and make
it's output argument. This approach also makes the code clearer.
load to assign variables inside of a nested function,
without explicitly specifying the variable name will generate an error. In the
load is used to load a MAT-file containing the
Y. Since the code does not explicitly declare that
Y is being assigned an error will be thrown.
function staticWorkspaceErrors function nest % This will error since var Y is not explicitly specified load MatFileWithVarY end end
To avoid an error, instead specify the variable name as an input to the
function noStaticWorkspaceErrors function nest % This will not error since variables 'x' and 'y' are specified load MatFileWithVarX x y = load('MatFileWithVarY','y'); end end
Alternatively, assign the output from the
load function to
a structure array.
While debugging, you cannot add a variable using the debug command prompt if you are stopped in a nested function. Assign the variable into the base workspace, which is not static.
Anonymous functions cannot contain variable assignments. When the anonymous function is called an error will be thrown.
% This will error since 'x' is being assigned inside % the anonymous function @()eval("x=2")
Rewrite the function in such a way that variable assignments are not required.
xEquals2 = @()2; x = xEquals2()
x = 2