18

I am running a script, but it is taking much too long so I want to terminate the script. However it has calculated a lot of data which I would ideally not want to throw away. Is there an alternative to ctrl-C with which you save the internal function variables to the workspace?

Ideally I'm looking for a Matlab keyboard shortcut like ctrl-C, but if that really can't be done maybe there is a way to do this in the script of my function. Any idea how to let my script react to ctrl-C as well, or maybe a GUI element which I can cancel and then I save the variables through my script?

Some similar questions I have found, but that don't answer my question:

close/pause

terminate

Different question, similar answer:

dbstop

EDIT:

This question is different because the problem the asker has is different: they want to know where the error is, which in my case Matlab already says. I just want to keep all data from working memory when it happens.

Community
  • 1
  • 1
Leo
  • 1,757
  • 3
  • 20
  • 44
  • I don't think you can do this: http://forums.codeguru.com/showthread.php?383556-Matlab-breakpoint-at-running-function gives some suggestions for the future though. You might consider periodically saving all your variables to a fie, then if you do get into this situation you could view that file after breaking. Doesn't help you today though. But it's a good feature request to send to Mathworks! – Dan Sep 25 '13 at 13:24
  • You can't catch ctrl-c in MATLAB. For alternative solutions, maybe this one helps: http://www.mathworks.com/matlabcentral/newsreader/view_thread/246784 – sebastian Sep 25 '13 at 13:29
  • possible duplicate of [running script stop in the middle](http://stackoverflow.com/questions/2140476/running-script-stop-in-the-middle) – horchler May 03 '15 at 18:43
  • @horchler Although their solution is the same, their problem was not. They basically want to know where an error originates, I want to save the data in working memory when an error arises. – Leo May 04 '15 at 10:58

3 Answers3

37

MATLAB versions 2016a and later

If you are using post 2016a versions of Matlab there is actually a pause button that appears when you run the script (as described by @pedre). This allows you to pause the script, inspect variables and then resume afterwards.

Make sure to check out the next section as this may still be convenient.

Older MATLAB versions

Actually the trick is to use dbstop if error.

First use this, then run your script. Once you introduce an error (for example, with Ctrl+C), you then have the chance to inspect/save your workspaces manually.

You will not be able to resume the script.

Community
  • 1
  • 1
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • After reading a lot of |`you cant do this`|'s, this is a much better answer than I expected, thank you! If I run this as the first command inside the body of a function, will it work as well? – Leo Sep 25 '13 at 13:40
  • 2
    @Leo I think you can call it any time before the error happens though it feels a bit strange to put debug commands in your regular functions. Personally I found my efficiency to go up significantly after I put this in my `startup.m`. (If there is an error that breaks my code I want to look at it anyway in 99% of the cases). – Dennis Jaheruddin Sep 25 '13 at 13:55
  • You can find these debug options in the GUI as well. Editor > Breakpoints. You can also break on warnings, or set breakpoints at specific locations in your script, and even conditional breakpoints. It is worth knowing how to use these. – Bernhard Feb 07 '17 at 14:36
  • Someone tried edit my post to tell me that the solution 'did not work' for 2015b. I don't have access to 2015b now, but would be very surprised if dbstop if error was suddenly taken out of commission. -- If you feel like this answer is very much incomplete, consider adding an answer so people may get the benefits and review it. – Dennis Jaheruddin Feb 23 '17 at 16:02
3

You just have to click inside your script so you can get the Editor page open, and then press pause, and see all intern values there.

enter image description here

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • 1
    There are many users using old versions of MATLAB (pre R2016a). I suggest you add a note saying that it was introduced in the R2016a release. Have a look at [this answer](http://stackoverflow.com/a/36074605/2338750). – Stewie Griffin Jun 21 '16 at 12:05
  • Welcome to SO, and thanks for answering! =) A little note: Sentences such as "I had the same problem..." etc are considered noise, since it doesn't really add any information to the answer. SO is a bit different than many other forums and q/a sites, since it focuses on the problem and answer alone, not the personal aspect. The reason for this is that questions and answers are there for _future visitors_, not the person asking the specific question alone. As an example, starting questions with: "Hi, I have a problem and have tried for 2 hours ...." is just noise for anyone reading the question. – Stewie Griffin Jun 21 '16 at 12:14
  • Since you can't add pictures yourself, I included a screenshot of the pause button. If you don't like my edits, there is a "Rollback" option in the edit section. You can edit the question by pressing "edit" below the answer. Anyway, +1 from me =) – Stewie Griffin Jun 21 '16 at 12:17
2

A colleague showed me an alternate way to incorporate this in my function, by attaching a save() command to the cancellation of a waitbar like so:

%appoint emergency file location
emergencysave = char(inputdlg({'fill in here:'}, 'windowtitle', 1, 'c:\defaultstringhere.mat'));

%or just emergencysave = 'c:\emergencysave.mat';



%create some GUI element you can cancel
times = 10;
wbinfo = struct('curlength', {0.0});
wb = waitbar(wbinfo.curlength);
wbinfo.wb = wb;



%attach save() to cancelling
anyimportantvariable = [];
for i=1:times
    anyimportantvariable = [anyimportantvariable, i^2];
    wbinfo.curlength = i/times;
    try
        waitbar(wbinfo.curlength, wb)
    catch
        save(emergencysave, 'anyimportantvariable');
        return;
end
Leo
  • 1,757
  • 3
  • 20
  • 44