I just discovered (to my surprise) that calling the following function
function foo()
if false
fprintf = 1;
else
% do nothing
end
fprintf('test')
gives and error Undefined function or variable "fprintf"
. My conclusion is that the scope of variables is determined before runtime (in my limited understanding how interpretation of computer languages and specifically Matlab works). Can anyone give me some background information on this?
Edit
Another interesting thing I forgot to mention above is that
function foo()
if false
fprintf = 1;
else
% do nothing
end
clear('fprintf')
fprintf('test')
produces Reference to a cleared variable fprintf
.
These may provide insight:
This can give you some info about what is shadowed:
(Below was confirmed as a bug) One gotcha is that Workspace structs, and classes on the path, have particular scoping and type precedence that (if you are me) may catch you out.
E.g. in 2017b:
MATLAB parses the function before it's ever run. It looks for variable names, for instance, regardless of the branching that activates (or doesn't activate) those variables. That is, scope is not determined at runtime.
ADDENDUM: I wouldn't recommend doing this, but I've seen a lot of people doing things with MATLAB that I wouldn't recommend. But... consider what would happen if someone were to define their own function called "false". The pre-runtime parser couldn't know what would happen if that function were called.
It seems that the first time the MATLAB JIT compiler parses the m-file, it identifies all variables declared in the function. It doesn't seem to care whether said variable is being declared in unreachable code. So your local
fprintf
variable immediately hides the builtin functionfprintf
. This means that, as far as this function is concerned, there is no builtin function namedfprintf
.Of course, once that happens, every reference within the function to
fprintf
refers to the local variable, and since the variable never actually gets created, attempting to access it results in errors.Clearing the variable simply clears the local variable, if it exists, it does not bring the builtin function back into scope.
To call a builtin function explicitly, you can use the
builtin
function.The line above will always print the text at the MATLAB command line, irrespective of local variables that may shadow the
fprintf
function.Interesting situation. I doubt if there is detailed information available about how the MATLAB interpreter works in regard to this strange case, but there are a couple of things to note in the documentation...
The function precedence order used by MATLAB places variables first:
Of course, in your example the variable
fprintf
doesn't actually exist in the workspace, since that branch of the conditional statement is never entered. However, the documentation on variable naming says this:This must be one of those "unexpected results", especially when the variable isn't actually created. The conclusion is that there must be some mechanism in MATLAB that parses a file at runtime to determine what possible variables could exist within a given scope, the net result of which is functions can still get shadowed by variables that appear in the m-file even if they don't ultimately appear in the workspace.
EDIT: Even more baffling is that functions like
exist
andwhich
aren't even aware of the fact that the function appears to be shadowed. Adding these lines before the call tofprintf
:Gives this output before the error occurs:
Indicating that they still see the built-in
fprintf
.