MATLAB: Is a function sometimes called when I try to use a variable with the same name as the function

functionMATLABnameorderprecedencescript

MATLAB 6.5 (R13) sometimes attempts to call a function when I am trying to use a variable with the same name as that function. There is no mention of this in the documentation.
I have written a function that calls a script. The script creates and initializes several variables. These variables are then used in the function. However, one of the variables has the same name as a MATLAB function, and that function is called when I try to reference the variable. The rules of precedence say that the variable should be used instead of the function.
For example, running the attached function 'foofunc' (given with fooscript below) will give the following error because MATLAB attempts to use the MATLAB function "day" instead of my variable "day" when resolving the statement 'x = day':
ERROR: ??? Error using ==> day
Please enter D.
Error in ==> D:\Applications\MATLAB\R13\work\foofunc.m
On line 3 ==> x = day;
Here is the example function, foofunc.m:
function foofunc
fooscript;
x = day;
Here is the example script file, called fooscript.m:
day = 3;

Best Answer

Documentation on using functions and variables with the same name can be found on the following website:
You can also see Related Solutions, and read the description below for more information.
The MATLAB parser attempts to bind symbols to either variable or function names when it can resolve the symbol as one or the other. However, in this case, MATLAB cannot tell if it is a variable that is being defined (because it gets created in a script, an eval statement, or via the load command, or assignin, or evalin, etc.) but it does see a function that matches the symbol, so it binds the function to the symbol.
To work around this issue, create the variable in the function. If the variable is created in the function and then initialized in the script, precedence works as it should.
For example:
Here is the fixed function foofunc_fixed.m:
function foofunc_fixed
day = [];
fooscript;
x = day;
And here is the same script fooscript.m:
day = 3;
Now when foofunc_fixed is called, the variable x is correctly assigned the contents of the variable day. The function call to day is thus avoided.