views:

62

answers:

2

Is there any way to stop the execution of a matlab program from the debugger like ctrl+c does, but then being able to continue execution (like you can in say c#)?

If not, is there any better way to workaround this other than trying to pre-emptively set break points or dbstop statements in your matlab code?

I would like to be able to interrupt a long running simulation to look at the current state and then continue the simulation.

The two options I'm currently using/considering are

  1. dbstop commands (or (conditional) breakpoints) in the code. Drawback is that sometimes I don't want to stop the simulation for a few hours, sometimes want to stop after only a few seconds (and I don't necessarily know that in advance) and this doesn't work well with this approach: If I set the break condition to break every 5 minutes, I can't leave matlab running for hours without interaction. If I set the condition to higher, I have to wait too long for the condition to hit.

  2. include code to save the workspace every few seconds/minutes and import the workspace into a second matlab instance. Drawback is that this is a huge hassle and also doesn't necessarily allows me to resume the simulation with the state of the saved workspace then step through the code for a few iterations.

I'm hoping there is a better solution than either of the 2. Thanks for any advice!

Edit: I think what I'm going to do is write simple matlab function that checks an environment variable or a file on disk every iteration and calls dbstop if I set a flag in this file or env. This way I can control when (and if needed which of several) the breakpoint hits from outside matlab by editing the file. Messy, but should work.

+1  A: 

You can set a conditional breakpoint in the MATLAB Editor. You can also use DBSTOP to do this. For example, this will set a conditional breakpoint in the file myFcn at line 20 which will stop execution when a loop variable i is a multiple of 500:

dbstop in myFcn.m at 20 if rem(i,500) == 0

Then you can continue execution after you inspect some of your variables.

gnovice
Thanks, this is pretty much what meant I'm doing in my option 1 in the question. But as I said, this has the drawback that I don't know after how many minutes I want to stop a priori. i.e. if the 500 in the condition corresponds to 5 minutes execution, I can't simply leave the simulation running for a few hours but have to interact with matlab every 5 minutes or the sim stops. Conversely if I change the 500 to say 5000 (50 min runtime), I have to wait 50 minutes until matlab stops for the first time...
Ben Schwehn
+3  A: 

This is not necessarily the best way, but you could simulate a file-based signal/interrupt framework. It could be done by checking every once in a while inside the long simulation loop for the existence of a specific file. If it does, you enter interactive mode using the keyboard command.

Something along the lines:

CHECK_EVERY = 10;    %# like a polling rate

tic
i = 1;               %# loop counter
while true           %# long running loop
    if rem(i,CHECK_EVERY) == 0 && exist('debug.txt','file')
        fprintf('%f seconds since last time.\n', toc)
        keyboard
        tic
    end

    %# ... long calculations ...    

    i = i + 1;
end

You would run your simulation as usual. When you would like to step in the code, simply create a file debug.txt (manually that is), and the execution will halt and you get the prompt:

2.803095 seconds since last time.
K>> 

You could then inspect your variables as usual... To continue, simply run return (dont forget to temporarily rename or remove the file). In order to exit, use dbquit


EDIT: Just occurred to me, instead of checking for files, an easier solution would be to use a dummy figure as the flag (as long as the figure is open, keep running).

hFig = figure; drawnow
while true
    if ~ishandle(hFig)
        keyboard
        hFig = figure; drawnow
    end

    %# ...
    pause(0.5)
end
Amro
I was actually just thinking about perhaps doing something along those lines :)Probably as good as it will get. Thanks!
Ben Schwehn
@BenSchwehn: idea = use a dummy figure instead of external files
Amro
I like the figure idea! Won't work when running on the command line , but on the command-line I rarely want to to debug anyway. Nice, thanks!
Ben Schwehn