is it possible to see what variables and functions affecting a certain variable ?
109 ビュー (過去 30 日間)
古いコメントを表示
hello everyone,
i am having a little problem, i need to figure out what affects one variable. that variable is in a for loop, which is in a function, which is also in a function and several values are also calculated by several other functions...
i do know i can have a chart what functions are involved and how they are affect each other, when i have a project. however, i cant see the variables there, or if i dont know how.
so thats my question, how can i see the functions AND variables that affect one variable.
12 件のコメント
Steven Lord
2024 年 8 月 15 日 20:39
Suppose you have a function that returns an output a and the "oracle" that you're looking for says that the value of a depended on inputs b and c. Suppose you also have a second function that returns an output a and that oracle says that the value of a depends on the input b but not on the input c. Specifically what action or decision are you going to take in handling the first of those functions differently from how you're going to handle the second function?
If you're going to use this as part of a decision making process, that says the first function is "better" than the second (or vice versa), what specifically about that information makes the first function "better"? What is your definition of "better"?
You still also haven't answered the question about what "affects" means in this context. Without that definition, I'd have to say that any variable could potentially / hypothetically affect any other variable by causing that second variable to be placed in a different location in memory, thus potentially causing it to be written in an area of a memory chip with some sort of flaw that prevents it from being recorded accurately. What about your definition of "affect" rules out this condition?
Here's another hypothetical example.
b = 42; % some arbitrary value
rng(b) % use b as the seed for the global random number generator
if rand > 0.5
a = 1;
else
a = 2;
end
The specific arbitrary value the variable b takes on will affect the output of the rand function and the output of the rand function controls whether a takes the value 1 or 2. Does that mean that b affects (by your criteria) the variable a?
What if b contains a path to a directory that appears in an addpath call and depending on which directory gets added to the path, the variable a gets evaluated differently. Does b affect a?
Walter Roberson
2024 年 8 月 16 日 1:04
a = 0;
for K = 1 : b
a = a + 1;
end
Does b affect a ? Logically yes.
a = 0;
for K = 1 : b
a = 1;
end
Does b affect a here? Logically no. Well, except if b(1) < 1 then it is an empty for loop, and the assignment of 1 to a would not be done, so indirectly b does indeed affect a, just not in any obvious way.
a = 1;
for K = 1 : b
a = 1;
end
Does b affect a here? No, other than the fact that errors would be generated if b is incompatible with numeric. So in theory
try
a = 1;
for K = 1 : b
a = 1;
end
catch ME
a = 0;
end
can result in the catch firing... so I guess there is a sense in which b affects a
回答 (4 件)
Walter Roberson
2024 年 8 月 15 日 17:51
Consider:
function a = moo(b,c)
a = 0*b + c;
end
Is a "affected" by b?
Under the normal course of events, one would say that the expression should be shortcut to being equivalent to
a = c;
However:
If b happens to be nan or inf, then 0*b is nan instead of 0, and that nan "poisons" the computation.
If b happens to be a symbolic variable, then 0*b will get simplified by the symbolic expression handler, regardless of the possibility that later b might become nan or inf.
So the analysis becomes: if b is provably not nan or inf (including if b is symbolic) then b has no influence on the expression and so should not be listed in any dependency analysis.
Consider:
f = FactorsOf(q);
if sum(f) == q
r = s;
else
r = t;
end
Is r affected by s?
Well, if q is odd, then sum(f) == q would be an "odd perfect number". It is not known whether any odd perfect numbers exist. It is known that if there are any odd perfect numbers, that they have value in excess of 10^2200 which is beyond 1.797e+308 that is the maximum double precision number, so if q is double precision and is odd then we can answer "No, s has no affect on r". Actually there are no representable odd double precision numbers beyond 9007199254740991 . But maybe we cannot rule out the possibility that q could be a symbolic number... In which case, to assert that r is affected by s (when q is odd) is to make a strong statement about number theory, that odd perfect numbers exist.
You can see from these examples that any analysis of which variables affect which other variables must necessarily be incomplete and inaccurate.
0 件のコメント
Harsh Kumar
2024 年 8 月 2 日 8:49
I think you could MATLAB Debugger.
- Set a breakpoint at the line where your variable of interest is used or modified.
- Run your code in debug mode.
- When the breakpoint is hit, use the "Step In" function to navigate through the code execution.
- Observe the Workspace window to see how variables change.
Abhas
2024 年 8 月 2 日 8:56
編集済み: Abhas
2024 年 8 月 2 日 9:09
Hi Andre,
To trace how a particular variable is affected throughout your code, you can use many different approaches such as MATLAB Debugger, "dbstop" and "dbstep", "disp" or "fprintf" by following the below steps:
- Set Breakpoints and Inspect Variables: Click on the dash next to the line number in the editor to set breakpoints at key points in your code. Run the code and use the workspace window or the command line to inspect the value of variables at each breakpoint.
- Dbstop and Dbstep: You can also use the "dbstop" and "dbstep" commands to control the debugger from the command line as follows:
dbstop if error % Stops execution if an error occurs
dbstop in function_name at line_number % Stops execution at a specific line in a function
- Disp or fprintf: Insert "disp" or "fprintf" statements in your code to print the values of variables at different stages.
You may refer to the following MathWorks documentation links to have a better understanding on the various debugging ways:
Matt J
2024 年 8 月 6 日 5:33
編集済み: Matt J
2024 年 8 月 6 日 5:33
Perhaps you are looking for matlab.codetools.requiredfilesandproducts.
2 件のコメント
Matt J
2024 年 8 月 15 日 13:47
No, you can't click on a variable, but you can make a wrapper function which returns that variable and trace its dependencies: Using your original example, tracing the Wrapper function below will allow you to determine that d was produced by a combination of function_a,b,c
function d=Wrapper(....)
a = function_a(b,c,d)
[b,c] = function_b(d,e,f)
d = function_c(c,f)
end
参考
カテゴリ
Help Center および File Exchange で Debugging and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!