This makes no sense to me.
This script returns the symbolic variable 'symb' as expected
funct1function funct1syms symbdisp(symb)end
Adding a nested function causes syms to return an error, even if the second function isn't called
funct1function funct1syms symbdisp(symb) function funct2 %nothing
endend
Error using assignin
Attempt to add "symb" to a static workspace.
See Variables in Nested and Anonymous Functions.
Error in syms (line 257)
assignin('caller', x, xsym);
Error in weird>funct1 (line 5)
syms symb
Error in weird (line 1)
funct1
However, if you assign a value to symb anywhere within the parent function, the error is avoided. Even if the assignment of symb is never reached by the function.
funct1function funct1syms symbdisp(symb) function funct2 %nothing endreturnsymb=0;end
Why? What is going on here? Why does adding a nested function to a function prevent you from creating symbolic variables, and why does defining the variable in an unreachable part of the parent function make it possible again?
Best Answer