This implementation of progressbar is intended to be extremely simple to use while providing a high quality user experience.
- Can add progressbar to existing m-files with a single line of code.
- Supports multiple bars in one figure to show progress of nested loops.
- Optional labels on bars.
- Figure closes automatically when task is complete.
- Only one figure can exist so old figures don't clutter the desktop.
- Remaining time estimate is accurate even if the figure gets closed.
- Minimal execution time. Won't slow down code.
- Randomized color. When a programmer gets bored...
n = 1000;
progressbar % Create figure and set starting time
for i = 1:n
pause(0.01) % Do something important
progressbar(i/n) % Update figure
Awadhesh: The comments at the top of the file show two ways of handling nested loops. You can adapt those to your situation. Here's one way to do it:
progressbar(0,0,0) % Init 3 bars
for i = 1:m
progressbar(,0,0) % Reset 2nd and 3rd bar
for j = 1:n
% do stuff
progressbar(,j/n) % Update 2nd bar
for k = 1:p
% do stuff
progressbar(,,k/p) % Update 3rd bar
progressbar(i/m) % Update 1st bar
I figure out how to place the text inside of the progress bar for a single progress bar. Comment out if length(progdata) > 1 and the end for the if. Also you can change 0.99 to width/2 to move the progtext location to the center of the bar.
Christopher: Good idea, but the problem with a cancel button is capturing the click and doing something right away while other code is busy running. See my old comment to "Jveer" for a bit more detail.
Francisco: I wrote and tested this code with R2010a (which is specified in the file info section at the top). I don't have an older Matlab release for testing, so you'll have to upgrade Matlab or patch the code yourself.
My previous comment got deleted somehow. Here it is again:
"Doesn't work for me, at least with multiple bars updated independently. The upper-level bars are never updated, only the innermost loop. The help example "% Simple multi bar" shows this. It DOES work if the bars are updated simultaneously.
Shame, because its a great piece of code.
I've got 126.96.36.1991 (R2008b)"
- The progress bar should be removed when the program finishes - even when interrupted (might be a hard one)
- A cancel button to interrupt the program might be handy right there
- the bar should integrate into the interface a bit better (docking - or in status bar of editor)
Otherwise - nice work!
Hey Benjamin. Your addition of embedding the progress bar into the GUI seems very interesting. How did you do that? Can you post or mail your file? Id love to see how its done(I didnt manage to do that).
I'm not familiar with MATLAB's parallel computing features, so I don't know for sure what's going on. My guess is that you can't update local figures from "workers", so you need to monitor their status somehow and update progressbar locally. Let me know if you figure out how to make it work.
Wow, really great work! Love it and it works like a charm.
For my purposes though, I took the liberty of editing around in your code a bit. What I did:
- Changed from function to class, so multiple instances can be run easily and without interfering with each other.
- Incorporated label changes during execution. The bar_text property can be edited and applies again when the figure updates.
- Made the calculation of the remaining time more accurate by taking the mean of all iterations already done as iteration time.
I could send you my modified file if you'd like to have a look!
Halil: There is no way to update labels without resetting the whole progress bar. I'd welcome code to implement that feature... :)
You're right about the remaining time calculation. I did it that way because of my concept of what multiple bars represent. I always think of the top bar as progress of the overall task, and other bars as progress of sub tasks. For an example of how I would use it, look at the "Fancy multi bar" demo in the progressbar's comments.
sorry about the mess, firefox failure. i wish there were a delete comment button...
anyways, first of all thank you for this efficient, fast, and handy function. i wonder whether it is possible to update the 'Label' of the bars or not. OR is it doable?
EDIT: also, one last thing as far as I can see, the 'remaining time' calculations only depend on the first bar's runtime but this is sort of misleading. maybe, the way how the remaining time is calculated can be optional (e.g. max/min of the remaining time of all bars or the bar(s) that will be used in calculations may be specified by the user or both options at the same time etc.) nevertheless, stating this calculation fact in the Help would be nice and explanatory.
btw, pardon me if i'm wrong about the way of time calculations.
Quite superior to waitbar. One feature I'd really like to see is the ability to update the progress bar label(s) without resetting the progress bar and timer, letting you inform the user exactly what process or subprocess is taking so long. Excellent job on this very handy function.
Jveer: I've played with adding pause and abort buttons but I was never totally happy with how it turned out. The main problem is that those buttons are unresponsive in the time period between calls to progressbar. You click, and then wait until the next call to progressbar before anything happens. In some of my own code, that could be 10 minutes or a half hour. And sometimes the click isn't captured so you never get the desired behavior at all. So far I've chosen to do without rather than add a half-broken feature.
lol, so simple and nice. makes ppl think you're awesome at matlab when they see a progbar :)
like this much better than waitbar!!! Nice work
13 Dec 2007
I made some modifications to this great app and you can find it here. Added an abort functionality and an output parameter so that when the progress bar is closed, you can use the output parameter to break from the for loop.
Have problem with this function and plotting graph.
This is the error that appear:
>??? Invalid handle object.
>Error in ==> progressbar at 202
>set(progpatch,'XData',[0 fractiondone >fractiondone 0])
>Error in ==> Untitled at 17
07 Aug 2006
Thak you for this substitute for waitbar.
I will be using it in software I am developing for my 4th year engineering report.
02 Jul 2006
This function is great and very simple to use.
27 Apr 2006
This thing is simple to use and looks fantastic. Thanks!!!
11 Feb 2006
I love it. So easy, so helpful.
05 Jan 2006
One of the beautiful codes I have seen!
Very much useful for programmers like me who have to wait for agonizingly long time to finish the code :))
06 Dec 2005
Great work. Very useful little piece. A possible addition is the ability to add another string during inititation which could specify the task at hand. Just my 0.02. Thanks a ton
11 Nov 2005
It is really terrific. Thank you.
If you like to have two, three or more progressbars, just copy the m-file to progressbar2, progressbar3, ...
06 Oct 2005
Thank you! Its just perfect! Now my program looks so good!
27 Sep 2005
An interesting extension would be to start more than one progressbars.
15 Sep 2005
19 Aug 2005
Jean-Vincent LE LAN
15 Jun 2005
Very simple and yet, very efficient ! It took me less than 2 minutes to add it successfully to my project
05 Apr 2005
Nifty little routine!
11 Mar 2005
22 Feb 2005
Antoni Josep Canós Marín
Very good!!. It should be included in future releases of MATLAB.
17 Feb 2005
16 Feb 2005
Very simple to use and very efficient. I like the ability to use it in one line of code.
08 Oct 2010
Major overhaul to support multiple bars and add labels.
08 Oct 2010
Major overhaul to support multiple bars and add labels.