When using a long script (that is used to call other scripts and functions), is it possible to find where a variable was set. By looking at the variable in the workplace I would like to be able to find the most recent place that it was set.
MATLAB: How to find where a variable was set
MATLABvariableworkspace
Related Solutions
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:
- do not use scripts; Or;
- do not use local variable names that are the same as the names of functions on the path; Or;
- 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.
error: Undefined function or variable "Test_G".
The error poped out because, the condition "if D==d1 Test_G=1" might have not obeyed and it is not set. It is sense that only that output will be displayed for which the conditions obey.
Best Answer