What you are asking to do is not recommended. It will be difficult to debug.
That said, it can be done using the evalin
function to reach out and get the current value of a
.
a=5;
foo = @(x)evalin('caller','a')+x;
foo(1) %Returns 6
a=3;
foo(1) %Returns 5
Better (much better!) would be to push the definition of a
into an apprpriate data structure or object, and create a function getCurrentValueOfA()
. Then you can define foo
as
foo = @(x) getCurrentValueOfA() + x;