Debugging a function line by line from another script
7 views (last 30 days)
Show older comments
Hello All,
Here is the minimum working example for what I am trying to do:
% main script to run a function
a = 1;
b = 2;
c = fun(a,b);
% script end
function out = fun(a,b)
d = 1;
out = a + b;
end
I would like to programmatically debug the function 'fun' from the workspace of the main script in such a manner that I can get the information about what is going on in each line of the function 'fun'. When I tried 'dbstop' right before the function 'fun' is called, debugger puts a break point at the first executable line inside the 'fun' and waits there. I would like to silently (meaning not being taken to breakpoint in 'fun') debug line by line through the 'fun' programmatically without leaving the workspace of main script. Is that possible ? Thanks in advance for your help. Any help is greatly appreciated.
-Anand
1 Comment
Guillaume
on 18 Jun 2015
That makes no sense to me. In order to debug the function, that is see what it does, you need to see the variables it is operating on, so you need to be in its workspace. Otherwise, what's the point?
Note that you can always switch back to the script workspace while in the function with dbup (and go back to the function workspace with dbdown). But your function won't have any influence on the script workspace (unless it's using globals or evalin and co.), so again there's little point in it.
Accepted Answer
Jan
on 18 Jun 2015
Edited: Jan
on 18 Jun 2015
No, this is not possible in Matlab.
But you can create a system to solve this more or less: Run a second Matlab session and a system to control it remotely (AutoHotKey, ActiveX, FEX: keyinject, FEX: textinject, java robot, etc.). So you will not debug the function locally in the main session, but in the remote session using the same data and let the results appear in the main session.
Well, such a construction would be far too complex to use it for a "debugging". It would be a kind of semi-autonomous Matlab-parser.
I strongly recommend to avoid such meta-programming. A program to run and debug another program, while the main program is still active - phew. This will be extremely confusing! What would happen in case of an error? Which parts should stop then? Or how do you want to prevent the debugged subfunction from stopping the debugger? What will happen, if the inspected subfunction calls the main function recursively?
Such meta-programming is a reliable marker for ideas, which can and should be simplified.
2 Comments
More Answers (0)
See Also
Categories
Find more on Database Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!