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

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 Comments

Define "affect". In the code below, do you consider the variable x to affect the variable y? Why or why not?
x = rand > 0.5;
if x
y = 1;
else
y = 2;
end
How are you hoping to use this information? Perhaps there's another way to achieve your ultimate goal.
as i mentioned, there are doezens of functions and some functions make use of other functions. i try to give simplified example, cause the whole thing is to much to post, thats why i didnt do it at first.
a = function_a(b,c,d)
[b,c] = function_b(d,e,f)
d = function_c(c,f)
so, now i have those functions and i would like to know if there is simething, that would show me dependacies starting from a backwards. in this case the functions a,b and c and the variables needed for those b,c,d,e and f and also that the variables c and f are being used twice by 2 different functions. also when functions are nested.
i just dont know how to come by nested functions on several occasions.
Suppose that inside function_b we assign the value 0 to b, using the values of d, e, and f only to affect the value of c. Do you still want to say that b is affected by d, e, and f?
Are those function calls listed in the order in which they're called? If so, a does not depend on e or f; the value of b that is used in the function_a call is not the one that would be generated when function_b will be called later on.
But let's take a step back. Assuming you're able to get this information, what are you hoping to do with it? What is your ultimate goal?
Can MATLAB generate something like a "calling tree" for a given code ?
@Steven Lord thats the thing, its way to many functions an too much going on inside the functions to answer these questions. if i can answer them easily, i wouldnt need to ask for something like that to begin with. that is the goal, to see what influence b,c and d have in function_a on a. if for example b has no effect in terms of a, then it doesnt matter, but i do not know that. if c doesnt effect a either, then the function_b would be also out of the picture (presumably), but i dont know that. so lets assume those doesnt have an effect on a, then i am searching for something that in thi case would give me something like this:
a is dependant of function_a -> which needs d to be calculated -> which needs function_c to be calculated -> which needs c and f -> c needs function_b to be calculated -> which needs d,e and f
so even when b and c arent used in function_a, c is needed for d in function_c and thus we need function_b as well. now imagine this 10 time bigger and more entangled. nightemarefule. thats why i asked if there is something that can track down one variable and how its been calculated or affected in any case by other variables and functions.
hmm, so i think if even @Steven Lord has no idea on this, then its not possible
You haven't answered the main question yet: what do you intend to do with this information? Do you want to see which lines of code you can remove while still getting the results you need?
The profiler should be fairly close to what you're asking, but depending on your end goal it may either need a small tweak, or is completely useless for your task.
maybe i dont know how to phrase i dunno. in my head i thought its clear what i want.
i want a list, or whatever, that enables me to see what variables in what functions are used in order to affect one certain other variable i choose.
the profiler does the whole script, but you cant pick one variable and see that there are a dozen other variables involved and also these functions.
hmm, now i try profiler, not that im wrong and it was always infront of me xD
Determining dependencies via static code analysis is impossible in general, only by executing MATLAB code can all code dependencies be determined. Confounding factors include runtime changes to the search path, runtime writing of script/function files, dynamic variable names, ASSIGNIN, STR2FUNC, FEVAL, etc. In general their effects cannot be determined until the code is executed.
What would such a dependency tracer do when a variable is computed by unnamed quantities. Consider -
function a=func1(b,varargin)
a=b+varargin{1};
end
function a=func2(varargin)
a=varargin{1}+varargin{2};
end
In func1, should a be traced back to b only, even though varargin{1} is used to compute it? And if so, does that mean that a has no dependencies in func2?
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?
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

Sign in to comment.

Answers (4)

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.
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 Comment

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

Sign in to comment.

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 Comment

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?

Sign in to comment.

Perhaps you are looking for matlab.codetools.requiredfilesandproducts.

2 Comments

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.
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

Sign in to comment.

Categories

Products

Release

R2024a

Asked:

on 2 Aug 2024

Commented:

on 16 Aug 2024

Community Treasure Hunt

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

Start Hunting!