Resolve Error: Attempt to Add Variable to a Static Workspace.
Issue
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.
For more information about the differences between the base and function workspaces, see Base and Function Workspaces. For more information about nested functions, see Nested Functions.
Possible Solutions
Declare Variable in Advance
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 makeX.m
dynamically
assigns a value to variable X
. A function that calls
makeX
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
Using eval
, evalin
, or assignin
to Assign New Variables In a Nested
Function
Using eval
, evalin
, or
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
Using a MATLAB script to Assign New Variables In a Nested Function
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
that 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 z
its output argument. This approach also makes the code clearer.
Use Explicit Variable Name with load
Function
Using load
to assign variables inside of a nested function,
without explicitly specifying the variable name will generate an error. In the
example below, load
is used to load a MAT-file containing the
variable 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
load
function.
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.
Assigning a Variable in the MATLAB Debugger in a Nested Function
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.
K>> assignin('base','X',myvalue)
Assigning a Variable in an Anonymous Functions
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