Variables within nested functions are accessible to more than just their
immediate function. A variable, x
, to which you assign a
value or use within a nested function resides in the workspace of the outermost
function that both contains the nested function and accesses
x
. Therefore, the scope of x
is the
function to which this workspace belongs, and all functions nested to any level
within that function.
If you intentionally use a variable in this manner, it is not a problem. For examples, see the MATLAB Programming Demo on Nested Functions.
However, if you unintentionally use a variable in this manner, it can result in unexpected behavior. If the highlighting indicates that the scope of a variable spans multiple functions, and that was not your intent, consider:
Renaming the nested function variable so it does not match the outer function variable name.
Passing the variable into the function as an input argument instead of using the variable directly within the nested function.
For more information, see Variable Scope in Nested Functions.