Borrar filtros
Borrar filtros

is it possible to see what variables and functions affecting a certain variable ?

109 visualizaciones (últimos 30 días)
Andre
Andre el 2 de Ag. de 2024 a las 8:38
Comentada: Walter Roberson el 16 de Ag. de 2024 a las 1:04
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 comentarios
Steven Lord
Steven Lord el 15 de Ag. de 2024 a las 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
Walter Roberson el 16 de Ag. de 2024 a las 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

Iniciar sesión para comentar.

Respuestas (4)

Walter Roberson
Walter Roberson el 15 de Ag. de 2024 a las 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.

Harsh Kumar
Harsh Kumar el 2 de Ag. de 2024 a las 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.
  1 comentario
Andre
Andre el 5 de Ag. de 2024 a las 6:28
thats what i used for now, problem is, its so many functions and several functions within other functions, that i lose track so fast. i tried to write it down on paper as i go through it with the debugger, but not long after its cluttered xD

Iniciar sesión para comentar.


Abhas
Abhas el 2 de Ag. de 2024 a las 8:56
Editada: Abhas el 2 de Ag. de 2024 a las 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:
  1. Set Breakpoints: https://www.mathworks.com/help/matlab/matlab_prog/set-breakpoints.html
  2. dbstop: https://www.mathworks.com/help/matlab/ref/dbstop.html
  3. dbstep: https://www.mathworks.com/help/matlab/ref/dbstep.html
  1 comentario
Andre
Andre el 5 de Ag. de 2024 a las 6:36
already using the debugger and breakpoints, but its hard to keep track, dozens of functions and variables in structs way more than that. its just not enough. and i dont know how other ppl do it, cause this project isnt big and others have much bigger projects. so how do they do that when they have to work on an unknown project and track something down?

Iniciar sesión para comentar.


Matt J
Matt J el 6 de Ag. de 2024 a las 5:33
Editada: Matt J el 6 de Ag. de 2024 a las 5:33
Perhaps you are looking for matlab.codetools.requiredfilesandproducts.
  2 comentarios
Andre
Andre el 15 de Ag. de 2024 a las 4:31
no, thats not it, i can get that in fancier when i use the function to display the dependancies when it is a project. however, at least to my knowladge, i cant "click" on one variable and look at the specific dependancies leading to that variable. your link does exactly what it says, lists all the required files for the m-file provided.
Matt J
Matt J el 15 de Ag. de 2024 a las 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

Iniciar sesión para comentar.

Categorías

Más información sobre Debugging and Analysis en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by