Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Matlab, is it possible to terminate a script, but save all its internal variables to workspace?

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.

like image 568
Leo Avatar asked Sep 25 '13 13:09

Leo


People also ask

Do workspace variables persist after you exit MATLAB?

Workspace variables do not persist after you exit MATLAB. To use your data across multiple sessions, save it to a compressed file with a . mat extension called a MAT-file. You can restore saved data by loading a MAT-file back into MATLAB.

Which command can be used to save all the variables in MATLAB?

Description. save( filename ) saves all variables from the current workspace in a MATLAB® formatted binary file (MAT-file) called filename . If filename exists, save overwrites the file. save( filename , variables ) saves only the variables or fields of a structure array specified by variables .

How do you end a script in MATLAB?

To stop execution of a MATLAB® command, press Ctrl+C or Ctrl+Break. On Apple Macintosh platforms, you also can use Command+.


1 Answers

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.

like image 107
Dennis Jaheruddin Avatar answered Sep 20 '22 14:09

Dennis Jaheruddin