MATLAB: Are previously defined variables not pulled into the anonymous function in MATLAB 7.0 (R14)

anonymousbindMATLABparserscriptundefined

I define a variable "p" in the base workspace either in the Command Window or by loading from a MAT-file:
p=666;
Then I create a script containing the following code:
x=@(g) g+p;
When I run the script, then try to use my anonymous function, I receive the following error:
x(49)
??? Undefined function or variable 'p'.
Why is this error occurring?

Best Answer

This bug has been fixed in Release 14 Service Pack 3 (R14SP3). For previous product releases, read below for any possible workarounds:
The error occurs because the parser is unable to bind a value to the 'p' variable when the script is run. With no value attached to it, the 'p' variable goes undefined inside the anonymous function. To work around the problem, you can assign p to itself before creating the anonymous function. Change the script to this:
p=p;
x=@(g) g+p;
and the anonymous function operates properly.