Enter debug mode if a variable changes value
Show older comments
I'm trying to debug somebody else's impenetrable matlab code; at the beginning of each iteration of a loop, I'm displaying the value of a variable x. Somewhere inside this loop the value of the variable has changed, and I can't figure out where this has happened. So I'd like matlab to "watch" this variable for me and enter debug mode at the point where it changes value. There have been a couple of posts related to this issue. @WalterRoberson appears to have provided one answer and @BrettShoelson appears to have provided another. Brett suggests using the
timer
command, which seems like an appealing solution. But I'm afraid both answers are too terse for me to understand how to implement them. Would somebody be willing to provide an example, e.g., using timer, that would pause my program when the value of some element of a vector x has changed? Thanks!
Accepted Answer
More Answers (1)
Jan
on 22 Mar 2018
dbstop in myprogram at 4 if X>=3.1415
Or you can insert some code:
x = 0;
X0 = x; % <-- inserted
for k = 1:1e6
if rand < 0.01
x = rand;
if x ~= X0 % <-- inserted
disp('Changed'); % <-- inserted, set a breakpoint here
end % <-- inserted
end
end
4 Comments
Leo Simon
on 23 Mar 2018
Walter Roberson
on 23 Mar 2018
Is the variable involved a global variable? Is it a shared variable? Is it possible that something is using evalin('caller') or assignin('caller') ? Or is it a variable in the base workspace? If it is in the base workspace, then do you have a GUI involved?
Leo Simon
on 24 Mar 2018
Walter Roberson
on 24 Mar 2018
As a first approximation:
At the point you want to start tracking changes, take a copy of the value and assign it to a variable in the base workspace. Then execute some utility code that finds all of the routines that use the variable on i/o and calls dbstop on the function name using a conditional of "if VariableName ~= evalin('base', 'ReferenceCopyofVariable')"
With breakpoints automatically created, proceed to execute.
This will not catch the change in value of the variable when it happens. What it would do is catch the first function call using the variable after it has been changed. You would then know that either the previous function call with the variable changed it or else that code between the two calls had changed it. It helps to narrow down the search a fair bit.
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!