MATLAB: Doesn’t MATLAB recognize the variable I have assigned in a script called from a function

functionMATLABoverwritingpoofingscript

In a function, I call a script that assigns various variables, one of which (tf) happens to be the name of a predefined MATLAB function. When I then evaluate the variable in the function, it defaults as the MATLAB function name rather than the variable.
Is there a good reason for this? Is there an optimal solution, other than changing my variable name?

Best Answer

Since R2015b, Mathworks has been getting stricter on the effects of assigning to a variable in a script and then using the variable in a function that invokes the script. Exactly what happens depends upon exactly which release you are using, but the current situation is this:
When current MATLAB parses a function, it looks at the names of all values that are clearly being assigned to in the function or passed into the function (or declared as global). If it sees a name that there is no obvious assignment to, then MATLAB will look on the search path to see if the name is the name of a known function. If it is the name of a known function, then MATLAB will assume that the reference is a call to the known function; if it is not the name of a known function, then it leaves it to be resolved at run-time.
If the function invokes a script that assigns to names, then if the name was not one of the known functions, then that assignment will provide the run-time resolution for the variable. However, if the name was the same as one of the known functions, then MATLAB will not change the name resolution in the function: the reference in the function will continue to be to the function.
There are three important work-arounds to this:
  1. do not use scripts; Or;
  2. do not use local variable names that are the same as the names of functions on the path; Or;
  3. before invoking the script, assign something to the variable name so that MATLAB knows to treat it as a variable.
Examples:
function my_function1
my_script; %assigns to tf

tf(1:3) %MATLAB will assume this refers to tf function

function my_function2
tf = []; %assign anything to it
my_script; %assigns to tf
tf(1:3) %refers to variable
Note that the same issues can now occur if there are assignin() calls, and can also occur if you load() a file that has the variable in it:
function my_function3
load abc %suppose it contains variable tf
tf(1:3) %MATLAB will assume this refers to tf function
The important work-around for load is to always assign the result of load to a variable and access the variable:
function my_function4
datastruct = load('abc.mat');
datastruct.tf(1:3) %access loaded variable directly
tf = datastruct.tf; %or extract it from the data structure
tf(1:3) %MATLAB can see the assignment to tf and so will not resolve to the function
Having variables "suddenly appear" without obvious assignment to them is often referred to as "poofing" the variable into existence. MATLAB is increasingly saying that if you do poof a variable into existence, then it might not pay attention.
Related Question