File Exchange

image thumbnail

Progress Bars

version (15.6 KB) by Richard Stapenhurst
Attractive and full-featured progress bars.


Updated 17 Aug 2010

View Version History

View License

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

Progress bars can be used to track long-running matlab programs where alternatives (e.g. displaying progress information in the console) are infeasible.
Progress bars can be group in windows, and organized independently or as a stack (for e.g. nested for-loops). Each bar displays a parameter name, the current value of that parameter out of a maximum, and an estimated remaining time for that bar.
The code is OO, so progress bar windows are created with
pr = Progress();
Organizing bars as a (FILO) stack makes it easier to add, update, and remove them:
pr.push_bar('parameter name', min, max);
where pr.set(value) sets the bar at the top of the stack. Alternatively, bars can be updated by name:
pr.set('parameter name', value);
and reset to their minimum value:
pr.reset('parameter name');

At each call to pr.set, timing data is collected, and a polynomial is fitted to the data, giving a good estimation of remaining time for processes that are of complexity O(N) or O(N^2). Higher degree polynomials can be used if a task is known not to be in these classes.
Closing a progress bar window manually (i.e. clicking on the X) produces an error - similar to ctrl-c.
The package also includes an automatic code generation tool for adding progress functionality with minimum fuss. This pre-processes an annotated matlab script and adds the necessary boilerplate code. Scripts can be annotated like this:

reptitions = 40;
for i=1:repititions %%p1
parameters_values = [1 2 3 5 10];
for j=1:numel(parameter_values) %%p2
end %%p2
end %%p1

where %%p# indicates a level of nesting, that will be reflected in the progress bar stack.

Furthermore, by including a (slightly awkward-looking) line at the top of a script:
the annotated script can be run as normal (i.e. by typing the script name) and the pre-processing will be invoked automatically - emitting the script with added boilerplate code into a temporary file, then executing this file in the base workspace, then deleting it. This makes the addition of progress bars quite transparent.

The latest addition to the functionality is that some custom code can now be executed upon request during execution. For example, when finding some results in a 100-repetition loop, we could include some code to plot the results for all of the repetitions so far. This lets us check up on the experiment whenever we like (e.g. see the results for the first 10 repetitions), without having to regularly plot graphs or wait until the entire experiment is complete. This can be easily achieved by adding this at the end of the original script:

for current_value=1:100 %%p1
results(current_value) = experiment(current_value);
end %%p1
%%finalise (this tag shows that all the following code should be executed when a button is pressed)

Lots of additional documentation and an example script are included.

Cite As

Richard Stapenhurst (2021). Progress Bars (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (11)

Fernando Gonzalez

Very impressed. Minimum of extra coding needed to make it work with nested for loops.

Jan Keller

Andrew O'Connor

Great feature using comments. Very clever.
+1 to including within functions. If possible it would be nice to control the progress bar within nested functions using only comments too. Not sure if this is possible.

Kenneth Eaton

Kenneth Eaton

Very nice submission. One small correction that's needed is to change "pr.set" to "pr.set_val" in the example in your above description.

Richard Stapenhurst

Thank you very much for your suggestions Jason, I have submitted an updated version.

Jason Kaeding

Hi Richard,

Documentation suggestion: you give no example of creating a progress bar manually. I had to look through the methods one by one to figure out what to call.

Additionally, because the set method is typically inherited from the handle class, I completely passed over this method when exploring, causing me to miss THE most important method for using the progress bars. Had I been advising you, I would have suggested using a function name of set_bar or something else to avoid such a name conflict. Nonetheless, because of this I strongly suggest some example in the main help so we can know how to get up and running.


Richard Stapenhurst

Hi Bryant,
As the description says, the pre-processing only works for scripts. I could modify it to accommodate functions/classes, but I'd be surprised if this was regularly useful.
However, it is possible to manually add a progress bar in non-script scenarios as described in the first paragraph of the description.
I will add support for pre-processing of functions to my to-do list :)

Bryant Svedin

This works great for simple scripts, but I am unable to get this to work with my GUIs or any script that has multiple functions in the same m-file.

Richard Stapenhurst

Thanks for your feedback Donald, if you could send me your test file then I can take a closer look at it.

It's possible that there is an error in your original test.m file.
When boilerplate code is added, a new file ("test_pr.m" in this case) is created and executed. Therefore, any errors in the original file (test.m) will be reported from the modified file (test_pr.m).

Ideally, I would like to catch and modify any exception thrown in test_pr.m so that it the stack trace and line numbers refer to code in test.m; if any MATLAB wizard knows if this is possible, I would be thrilled to know how! (I would want to be able to generate an exception that looks like it came from a specific line in a specific file)

Donald Lacombe

For some reason, I keep getting the following error:

??? Error: File: test_pr.m Line: 50 Column: 25
Unbalanced or unexpected parenthesis or bracket.

Error in ==> prog at 47
evalin('base', new_file(1:end-2));

Error in ==> test at 1

However, it does work with other simple loop examples and is very nice!

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

Community Treasure Hunt

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

Start Hunting!