MATLAB Answers


How to find where a variable was set?

elspeth storey さんによって質問されました 2019 年 4 月 2 日
最新アクティビティ John D'Errico
さんによって コメントされました 2019 年 4 月 2 日
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.

  1 件のコメント

Stephen Cobeldick
2019 年 4 月 2 日
Better code design would go a long way to avoiding these kind of problems (e.g. using functions rather than hard-to-debug scripts which all share the same worskpace).

サインイン to comment.




2 件の回答

Image Analyst
回答者: Image Analyst
2019 年 4 月 2 日

I believe there is a way you can call dbstop to stop at any line of code where/when a variable changes.

  2 件のコメント

Stephen Cobeldick
2019 年 4 月 2 日
From the dbstop documentation:
dbstop in file if expression
But it better code design would be a much better solution (e.g. functions rather scripts).
John D'Errico
2019 年 4 月 2 日
Yes, IF you run the code, then suppose the variable X is initially set to zero, that is to some known value. It would require you to run the code, rather than merely examine the code.
It looks like you can use the debugger to test if a specific event ever happens. For example, assume a script where X is initially set to 0.
The help for dbstop offers this option:
(10) dbstop in FILE if EXPRESSION
So if the expression is (X ~= 0), a stop will occur on the line where X is changed. But that will cause a stop at the first event where X is altered.
But now finding the next point where X is altered will get more difficult, or most importantly, the LAST point.
It would seem to be tricky.

サインイン to comment.

John D'Errico
回答者: John D'Errico
2019 年 4 月 2 日
編集済み: John D'Errico
2019 年 4 月 2 日

Not very easily. Depending on how nasty and convoluted the code is, you can easily have something that is essentially impossible to determine where any variable is/was last set, because your code might use any number of things like recursive calls, global variables, even variable names that were created using eval or evalin. Of course, the use of eval can really screw things up, because you will then not even find code where you ever see the variable name on the left hand side of an assignment. If you want me to be really creative, I can programmatically create a variable name at random. So at some arbitrary, random point, the variable might be created, as if a monkey were typing variable names at random. Eventually, the variable X might be created, or possibly not, since the programmatic monkey is doing uncontrollable stuff.
Do you want me to write a piece of code that has only about 4 lines, yet you will not be able to identify where a variable was last defined, thus on which specific line of code?
Thus, consider code that has a simple branch in it. I might initially define the variable X to be 0. But inside an if statement, X will be reset to have the value pi.
X = 0;
if test
X = pi;
So it might seem the answer is easy. The variable was last defined inside the branch. But what if the branch test is never satisfied? Or what if that test is based on a random number? Or what if the test is based on the result provided by user interaction from an input statement?
My point is, I can create a very simple circumstance where it is literally impossible to decide where the variable was last set, and therefore, what would be the current value of the variable. Does X have the value of 0, or is it pi? Like Schrödinger's cat, you cannot determine whether X has either given value, merely by looking at the code from the outside. Without evaluating the code itself, essentially evaluating every line of code one at a time, you can never determine where that variable was set. And if there is anything of a truly random nature involved (e.g., fisson of an arbitrary U235 atom), then you can provably not determine what happened merely by looking at the code.

  0 件のコメント

サインイン to comment.

Translated by