Debugging a function line by line from another script

7 views (last 30 days)
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
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.

Sign in to comment.

Accepted Answer

Jan
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
Anand
Anand on 18 Jun 2015
Thank you very much for your answer. The reason I considered such a meta-programming approach is because the functions I want to test, have a lot of code that interacts with a MySQL database. I would like to go through each line of code in the function and assert if it is manipulating the data on the MySQL database the way we want it to do and create a report in the end showing when manipulation didn't happen as expected. For example if the line 10 of the function reads the value for the variable 'x' from MySQL database, we would like to inject a random value (say 39) in MySQL database for variable 'x' before line 10 is executed, then execute line 10 and check if the execution of line 10 indeed returned the value 39.
Walter Roberson
Walter Roberson on 18 Jun 2015
The latter part of your description calls for a Unit Test framework.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!