MATLAB: Do I receive an error when trying to access a loaded variable in a function in MATLAB 7.10 (R2010a)

functionloadMATLABscriptshadowedvariable

While in a function, I am loading a variable from a .MAT-file and then I try to access that variable. If that variable name is also available as function, I receive an error. As an example:
function pp
x.debug = 'abc';
save a.mat -struct x
load a.mat
whos
fprintf('%s\n', debug);
end
With this example I receive the following error:
??? Attempt to execute SCRIPT debug as a function:
c:\MATLAB\R2010a\toolbox\matlab\codetools\debug.m
Error in ==> pp at 7
fprintf('%s\n', debug);

Best Answer

This is expected behavior in MATLAB 7.10 (R2010a). MATLAB determines whether names in a function are variables or functions before executing the function. In the given example MATLAB is unaware of the variable "debug", so it uses "debug" as a function. As a workaround you can specify which variable or variables should be loaded:
function pp
x.debug = 'abc';
save a.mat -struct x
load a.mat debug
whos
fprintf('%s\n', debug);
end