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” on page 20-9. For more information about nested functions, see “Nested
Functions” on page 20-27.
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
Resolve Error: Attempt to Add Variable to a Static Workspace.
20-33
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” on page 20-9. For more information about nested functions, see “Nested
Functions” on page 20-27.
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
Resolve Error: Attempt to Add Variable to a Static Workspace.
20-33
