View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Pause a long MATLAB run with a simple click.

4.8 | 5 ratings Rate this file 30 Downloads (last 30 days) File Size: 730 Bytes File ID: #35114 Version: 1.2
image thumbnail

Pause a long MATLAB run with a simple click.


Roni Peer (view profile)


16 Feb 2012 (Updated )

A simple way to pause a long simulation, to check progress, without expensive breakpoints or checks

| Watch this File

File Information

Sometimes you create MATLAB functions that take ages to run (more than 10 minutes). You want to be able to pause every now and then, by clicking a "CTRL+P" or something like that, make sure all the values are ok, and then continue running from the same spot. If you tried "CTRL+C", you know it's not working well. So, untill such an option will exist, I wrote a simple example to show how you can use a PushButton and a guarded breakpoint to do so.
Run the file, by executing "myFunc(1)".
You will see a button pop up. Every time you press it, it will halt the function's execution and allow you to view the progress of the code.
Pressing F5 will continue the run.
You can use this as a template for your specific needs, simply modify and use.
PLEASE NOTE: In MATLAB version R2016a this feature is already a part of the editor, so you don't really need this submission anymore. Hooray for MathWorks development team for adding this feature!


This file inspired Soft Interrupting Of Long Computer Runs.

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
13 Oct 2016 Xiaoyu Qian

I'm now running a script. It takes a long time. I want to pause it whenever I want and see the partial result. If the partial result is not good enough, I want it to continue running. Can I do it with this file? To be honest, I'm a little confused with this file. Could anyone please explain it in detail? I appreciate it a lot!

Comment only
19 Apr 2016 Roni Peer

Roni Peer (view profile)

Thanks for the comment, I've updated the submission to reflect this.
Btw, interesting point:
Running on R2015b, the code "myFcn(1)" takes about 1000 seconds to run.
The same code on R2016a takes 2 seconds!
Seems like there was something strange with 15b and was fixed in 16a.

Comment only
05 Apr 2016 MertLab

Works great. Like Stefano suggested, using close(h) while running functions is better than clear h to close the pause button.

05 Aug 2015 Gero Nootz

Works well. It would be nice if the script would resume after pressing the button a second time (e.g. execute “dbcont”). Thanks for sharing it.

21 May 2015 Aamir

Aamir (view profile)

29 May 2014 Stefano

Really useful! Thanks!
The last lines of myFcn(u) should actually be:

clear h

So that the "pause" plot window is closed and h is released.

22 Feb 2013 Seban

Seban (view profile)

Works like a charm :)

21 Feb 2012 Roni Peer

Roni Peer (view profile)

Good suggestion, Jan. I've modified the files so they reflect this.

Comment only
20 Feb 2012 Jan Simon

Jan Simon (view profile)

What about calling KEYBOARD in the callback of the button? Then there is no need for a GLOBAL variable or setting a breakpoint. See "help keyboard".

Comment only
21 Feb 2012 1.1

Removed unnecessary FLAG checks.

19 Apr 2016 1.2

- Changed the "clear(h)" command to "close(h)" in order to close the button when the simulation finishes.
- Updated the description, to reflect development effort that were added in R2016a version.

Contact us