File Exchange

image thumbnail

STOPLOOP (v1.0, jun 2008)

version 1.0 (2.07 KB) by

Shows stop button to interrupt loops

19 Ratings



View License

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?".

Comments and Ratings (24)

hammad Ahmad

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

Eric Chen

Paul Gresham

Thankyou Jos!


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!

Matt J

Matt J (view profile)

Samuel Pedro

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

Martina Callaghan

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.


Chandler (view profile)

  • 1 file
  • 3.88889

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.


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!


It was very useful to me. Thanks Jos

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.

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.

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.

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 !


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.

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.

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' =).


Jos (the author)

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

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.

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!

E. Fudd


MATLAB Release
MATLAB 7.0.1 (R14SP1)

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

» Watch video