File Exchange

image thumbnail

Display progress, pause or stop a time-consuming loop

version (93.8 KB) by Rafael Oliveira
Allows you to display a loop’s progress, pause (and also change related code) or stop it.


Updated 17 Feb 2012

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This class creates a small pause/stop window, that allows you to either abort a running loop or pause the execution and create breakpoints or change/correct mistakes in the code (this works for files that are not in the stack from where stop.requested is called).

It can be used in different ways:
- Control the loop execution:
while ~stop.requested
%do your heavy calculations here

- abort execution in the middle
for i = 1:100
%do your heavy calculations here
if stop.requested
error('Stop requested')

- inform the current progress
iter = 1;
maxIter = 1000;

while iter<maxIter || stop.requested
%do your heavy calculations here
progress = iter/maxIter;

In loops with many iterations, you can also avoid updating the screen too often (and reduce the impact of calling stop.request), by calling: stop.requested(num); where num is the interval on which the gui will be refreshed to check for user interaction.

And to enable the easter egg (, use:
stop.setStatus('xkcd', progress);

Cite As

Rafael Oliveira (2021). Display progress, pause or stop a time-consuming loop (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (1)

kasim tasdemir


I use 'dispstat' function just for this purpose. It can update the previous output which is a missing function of default 'disp'. Very simple to use. It can be downloaded from here:

***Sample usage:
dispstat('','init'); % One time only initialization
dispstat(sprintf('Begining the process...'),'keepthis','timestamp');
for i = 97:100
dispstat(sprintf('Progress %d%%',i),'timestamp');
%doing some heavy stuff here

11:25:37 Begining the process...
11:25:37 Progress 100%

All the best

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: xkcd1930

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!