View License

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

» Watch video

Highlights from
STOPLOOP (v1.0, jun 2008)

3.9 | 19 ratings Rate this file 15 Downloads (last 30 days) File Size: 2.07 KB File ID: #20455 Version: 1.0

STOPLOOP (v1.0, jun 2008)


Jos (10584) (view profile)


25 Jun 2008 (Updated )

Shows stop button to interrupt loops

| Watch this File

File Information

STOPLOOP - creates stop button to have a user interrupt a loop
FS = STOPLOOP creates a message box window and returns a structure FS that
holds two functions, called FS.Stop and FS.Clear. The function FS.Stop()
will return true, if the OK button has been clicked (or the message box
has been removed), so that a loop can be interrupted.

The function FS.Clear() can be used to remove the message box, if a loop has ended without user interruption.
FS = STOPLOOP(STR) uses the string STR to display instead of the default
'Stop the Loop'.
     tic ; % We will measure elapsed time in a loop
                   % Set up the stop box:
     FS = stoploop({'Stop me before', '5 seconds have elapsed'}) ;
                   % Display elapsed time
     fprintf('\nSTOPLOOP: elapsed time (s): %5.2f\n',toc)
                   % start the loop
     while(~FS.Stop() && toc < 5), % Check if the loop has to be stopped
         fprintf('%c',repmat(8,6,1)) ; % clear up previous time
         fprintf('%5.2f\n',toc) ; % display elapsed time
     FS.Clear() ; % Clear up the box
     clear FS ; % this structure has no use anymore

Version 1.0, jun 2008
Inspired by several posts on the FEX on "how to interrupt a loop?".

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
20 Jan 2017 hammad Ahmad

bravo...... wasted a lot of time on internet but then finally found it... hahaha...

22 Jul 2016 Eric Chen

19 Mar 2016 Paul Gresham

Thankyou Jos!

11 Mar 2016 Jean-Philippe Girard

10 Nov 2015 David

David (view profile)

I spent hours tonight looking for something that could interrupt a loop in a controlled fashion, and this works great. Thank you Jos!

19 Aug 2015 Matt J

Matt J (view profile)

16 Feb 2015 Samuel Pedro

This is a very simple, yet very helpful feature. Thank you.

26 Feb 2014 Daniel Herrera

01 Jun 2011 Martina Callaghan

01 Apr 2011 Daniel Armyr

Daniel Armyr (view profile)

Just what I realize I have needed the last few weeks. Recent versions of Matlab have started behaving strange when pressing Ctrl-C in a loop that draws complex figures and this file solved that problem.

This is a nice little function that I find well-written, and well documented. You don't get a 5 because it still has some issues like being overdrawn by plot commands unless one is carefull with handles, but over all I am very happy, and this one will end up in my personal toolbox of good-to-have things.

08 Dec 2009 Chandler

Chandler (view profile)

Hi Jos,

I enjoyed your program but I'm looking to tackle a little different problem. I have a loop and I want to jump to the next iteration of the loop if a certain amount of time has past. I'm not sure how to do this. I'd appreciate any help you can provide.

04 Jan 2009 Mike

Mike (view profile)

Relax, have a home brew. Free country with Free opinions to agree/dissagree with.. All problems are hard problems until they are solved. Thanks for the submission!

28 Dec 2008 Khurram

It was very useful to me. Thanks Jos

30 Jul 2008 Thierry Dalon

Wesley: You can see other contributions in the FEX of waitbar or progressbar with abort button
(example FEX 3607). In that case I don't see why you would like 2 GUIS; the progressbar with abort would be enough, wouldn't it?
Else I don't think what Jos publish is "trivial" or "useless". @Jos: keep on contributing to FEX! I am a fan of some of your contributions and have leanrt a lot from your efficient master coding.
Cheers. don't let you bothered by stupid guys.

Comment only
25 Jul 2008 Wesley Hampton

How about adding a progress bar to the stoploop message box to track the progress of the actions in the main gui loop and demonstrate how to communicate between two running guis.

10 Jul 2008 Dick Laughlin

This is great. I have been fighting this problem for several year. My latest application is to preview video and then grab a frame when I see what I am looking for. Previously I either used a break (which did not clean up the files) or had to write a GUI.

30 Jun 2008 S Slav

I thinks this is very useful and something similar should be part of figures by default!!
Everybody knows, everything is trivial, as well as nothing !


27 Jun 2008 Jos (the author)

If one thinks this is trivial, he or she has the right to say so. Preferably with some specific comments why, otherwise such a review is rather useless.

Again, I myself (being somewhat biased of course) do not think STOPLOOP is that trivial. It can offer a solution to a common problem users may be faced with. Matlab's own MEAN, for instance, is also quite trivial (given SUM and SIZE), but I suspect that many think it has its merits as well ... Moreover, others could incorporate the ideas presented in this submission into their own solutions.

Note that I have tried to make it as user friendly as possible, including a help, an example and internal comments, making it more valuable than just saying: create an object and monitor the userdata .... I hope that others may benefit and possibly learn from this contribution to the FEX, and I would like to see some other thoughts and criticisms.

Comment only
26 Jun 2008 E. Fudd (shhhhhh)

Oh, I see. We aren't allowed to critique without attribution. Fine. I take back my comments. The code is wonderful and a work of art. All submissions will recieve a 5 for now on.

26 Jun 2008 Ken Eaton

Hey Jos,

Don't ya just love snarky commenters who are afraid to use their real names for fear that someone will judge their submissions as harshly as they judge others' =).


Comment only
26 Jun 2008 Jos (the author)

Apparently the problem is not trivial to others ... but, anyway, thanks for your thoughts.

Comment only
26 Jun 2008 E. Fudd

Sure. Its quite trivial to set a flag in the userdata attribute of some convenient object and then get that attribute during the loop execution. Once the flag is encountered a simple break will suffice. As I said, trivial.

26 Jun 2008 Jos (the author)

Thanks E.F. for your helpful review. Maybe you can tell us why you deem this as trivial? Note that it does not mimick ctrl-C!

Comment only
26 Jun 2008 E. Fudd


Contact us