Matlab GUI Tutorial - Progress / Status Bar
13 Dec 2007 Quan Quach 38 comments 9718 views
Introduction
While matlab is running through a long function or for loop, the user has no idea when it will be completed. By implementing a progress bar, the user will be able to see the status of the function and when it will be completed. In this tutorial, you will learn how to implement a progress bar to your GUI, script, or function. This command is typically used inside a for loop that performs a lengthy computation. While it is possible to use the waitbar command in matlab, it is not as flexible as this custom progress bar for the following three reasons:
-
Does not show percent completed
-
Does not show time left before completion
-
If user closes wait bar before it is completed, an error will result
Matlab Wait Bar:

Modified Wait Bar:

How to use the Progress Bar
-
Download the m-file here by right clicking on the link and selecting the “Save as” option (This code was originally deveopled by Steve Hoelzer and you can find the original m-file here).
-
Place the m-file in whatever folder you desire and set the Matlab Current directory to wherever you saved the m-file.
-
The progressbar function takes in two arguments. The first is the fraction of the task that is completed, so the input is between 0 and 1. The second argument is for positioning purposes:
Position determines where the progressbar figure appears on screen. The progress bar’s position can be specifed as follows:
- [x, y] - Position of lower left corner in normalized units (0.0 - 1.0)
- 0 - Centered (Default)
- 1 - Upper right
- 2 - Upper left
- 3 - Lower left
- 4 - Lower right
- 5 - Random [x, y] position
The output parameter is used mainly to break from the for loop if necessary. While the progress bar is active on the screen, the output parameter is equal to 0. When the user closes the progress bar, the function returns 1.
-
Now, lets run a quick example using the progress bar. Cut and paste the following code into the the Matlab command prompt
clear all for x=1:100000 stopBar= progressbar(x/100000,0); if (stopBar) break; end end
-
Here’s what you should see:

-
If you try to close the progress bar before it gets to 100% you will receive the following prompt:

If you decide to close the bar, it will cancel the bar and will exit the for loop, stopping all computations within that for loop.
-
If you don’t like the color of the progress bar, you can modify the code to change its color. The line you are looking for is on line 190. You can mess around with the number array to give you the desired color:
190
set(progpatch,'FaceColor',[.1 1 .1]);
-
If you’re using this progress bar in conjunction with a GUI, you should check out this post on how to disable buttons. It works great with the progress bar because it doesn’t allow the user to push any buttons while the data is being processed.
This is the end of the tutorial.
38 Responses to “Matlab GUI Tutorial - Progress / Status Bar”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


That looks like a really good feature to add. And, it’s nice to know that someone is making use of my code.
Ohhh, nice. Just what I am looking for. Thx Quan and Steve for post and code.
Are you sure this works (well, I am it does not)?
1) line 258 is a comment line but misses the % sign
2) Killing the figure produces this error
??? Undefined function or variable ‘closeBar’
3) Changing line 200 to
set(progfig,’CloseRequestFcn’,@closeBar)
and the definition of closeBar to
function closeBar(obj,evt)
avoids the error but, … killing the figure wont stop it
4) What’s the meaning of this piece of code?
if (isempty(progfig) && ~(fractiondone==0))
delete(progfig) % Close progress bar
Joaquim:
1)line 258 is indeed a comment line, but that function isn’t called, so there shouldn’t be an error. anyhow, I went back and fixed this.
2) I’m not sure why you’re getting that error, but it works okay for me when I used the sample code:
clear all
for x=1:100000
stopBar= progressbar(x/100000,0);
if (stopBar) break; end
end
3) You can also define it that way. I did exactly that and it still works for me.
4) That code checks to see if the progress bar is active. If it is, then that object is deleted and the function returns a value of 1 as its output.
Quan Quach
I think the problems I reported happens on R13. For example the current line 249 still give an error
set(progfig,’CloseRequestFcn’,@closereq);
??? Error using ==> closereq
Too many input arguments.
Regarding my previous point 4)
“if (isempty(progfig) && ~(fractiondone==0))
delete(progfig) % Close progress bar”
You are deleting an object - progfig - … which is empty. Notice that line is only executed if
if (isempty(progfig) …
Joaquim:
I use Matlab 2007a, so that may be why it isn’t working for you. Sorry.
This is a great function, once I got it to work on my system using R2006b.
I had problems with the progressbar not closing after reaching 100%.
After applying the following changes, starting at line 245, it works as expected
if percentdone == 100 % Task completed
delete(progfig) % Close progress bar
%change the close request function back to normal
%set(progfig,’CloseRequestFcn’,@closereq);
% Clear persistent vars
clear progfig progpatch starttime lastupdate firstIteration
return
end
Joaquim,
I’m glad you got it to work. I coded it in such a way that the user had to manually close the progress bar when it reaches 100%. But I can see how some people will want it to automatically close when it reaches 100%. I’m glad you got it to work.
Quan
Quan,
Nice work to both Steve and yourself.
A couple of things re: some comments that have been left here.
1. to resolve the error
??? Error using ==> closereq
Too many input arguments.
I changed
set(progfig,’CloseRequestFcn’,@closereq);
to
set(progfig,’CloseRequestFcn’,’closereq’);
(Matlab R14 SP3)
2. (pretty obvious) Everyone can choose “autoclose” or manual by first adding an argument “autoclose” to the function, then changing the “if 100%” block to
if percentdone == 100 % Task completed
if autoclose
delete(progfig) % Close progress bar
else
%change the close request function back to normal
set(progfig,’CloseRequestFcn’,’closereq’);
end
% Clear persistent vars
clear progfig progpatch starttime lastupdate firstIteration
return
end
Cheers
Dylan,
Thanks for the fix. I edited the m-file to include your fix. I mistakenly did @closereq when it should have been ‘closereq’ on line 249, and I used ‘closebar’ when it should have been @closebar on line 202. Currently, I’m using Matlab R2007a, but it should work for other versions.
Thanks for the autoclose snippet as well! I’m sure others will find it useful
Quan
Adding this feature really enhances the efficiency of matlab codes and GUIs.
Quan,
do you see a way of applying a status bar to the ’sim’ function showing the progress of the running Simulink simulation?
Thanks!
Hello Bertin,
I’d have to look into that, as I’m not sure how to do it as of now. I don’t really use simulink that much either. If I do find a way, I will be sure to post it here.
Quan
Hello Quan (and all the others who might be interested),
Found a way with an embedded Matlab function, which works really well.
On MatlabCentral you can have more information by searching for ’simulink waitbar’.
Thank you for your help and quick reply anyways.
Great site you have there!
Hello,
Nice improvements. Can I suggest a new feature: adding the possibility to put 2 or 3 different progress bars on the same figure.
regards
Hi Claude, that’s a good idea. I’ll take a look into it and see what I can do. Or if anyone wants to, they can give it a shot and contact me if they can pull it off!
Hi there. I’m getting this error message when call the function
What am i doing wrong?
??? Invalid handle object.
Error in ==> progressbar at 227
set(progpatch,’XData’,[0 fractiondone fractiondone 0])
The version of matlab i used is 2006b and 2007a. Both work. What version are you using qresho?
i am using 2007a. When i use this function in workspace with your example code, it works. But when i use it in a for loop in M-file it fails.
for loop is something like this
for i=1:size(matrix,2)
….
….
processbar(i/size(matrix,2))
end
Can you use the contact form and send me the portion of code that is giving you the error? I want to try to recreate it on my system
Quan
i used the contact form. Did you receive my code?
sorry, my bad, i was typing processbar and not progressbar. Stupid me. SOrry
Error: File: D:\yang\project\status bar\progressbar.m Line: 118 Column: 23
Expected a variable, function, or constant, found “&”.
Hi
It worked for a while but then I stared getting (with 2007a)
—
??? Invalid handle object.
Error in ==> progressbar at 228
set(progpatch,’XData’,[0 fractiondone fractiondone 0])
—-
I had changed the “% delete(progfig) % Close progress bar” to “delete(progfig) % Close progress bar” in order to close it after 100%. I finally changed the section to:
—
if percentdone == 100 % Task completed
%change the close request function back to normal
set(progfig,’CloseRequestFcn’,'closereq’);
delete(progfig) % Close progress bar
% Clear persistent vars
clear progpatch starttime lastupdate firstIteration %progfig
return
end
—
And now it works fine
It didn’t work fine for all eternity. I noticed that the error:
—
??? Invalid handle object.
Error in ==> progressbar at 227
set(progpatch,’XData’,[0 fractiondone fractiondone 0])
227 set(progpatch,’XData’,[0 fractiondone fractiondone 0])
—
is produced if progressbar was run with some error prior to this time… two solutions might be to run progressbar(1) or resaving the file i.e. resetting all persistent variables
great work guys just a small comment
with bad codes can actually skipp 100% lols
for auto closure some guys
better to use
if percentdone >= 100 % Task completed
delete(progfig) % Close progress bar
thanks again for your great help.
using it in a Multi Gravity Assist flyby solver
Great code but seem to only work when there is only numerical calculations. if there is a plot() in the for loop then…….
??? Error using ==> set
Invalid handle object.
Error in ==> progressbar at 227
set(progpatch,’XData’,[0 fractiondone fractiondone 0])
Error in ==> Model_004 at 214
progressbar(j/Final)
Any ideas or workarounds?
best.
Excellent script! Thank you very much for sharing!
I use 2 different handles for 2 different progressbars.
Like one bar shows me the time left and the others shows a numerical iteration. Thats what I am using it for.
Any idea how i can run them at the same time ? I mean 2 both bars on screen and running.
Thanks in advance !
oh it was my fault sorry. The bars were already running, just one was always positioned behind the other so i couldn´t see it. Sorry !
Hm does anyone have an idea how the progressbar work when there are other plots in the for..loop ?
I always get the
Invalid handle object.
Error in ==> progressbar at 227
set(progpatch,’XData’,[0 fractiondone fractiondone 0])
thx !
After the operation we close the progressbar window by hand. Is it possible to close the window automatically? Does any one give hint?
Hi NR, see comment 9
Hi, Joe
You may need to use clf, to clear the current figure for the program to work.
Nice website…
Hi, i wan to ask, what if i want to make a progress/wait bar from function which has not loopong function ‘for’ inside, but it do have looping, such as the matlab tool: colfilt. If i have a code like this:
meanI=colfilt(I, [w w], ’sliding’, @mean);
‘I’ for a big size image, ‘w’ is window size for slicing, and ‘@mean’ is when i want to get the local mean each windows.
It took about 30 seconds, how can i make that progress bar??? Remember i have not use ‘for’ loop.
Thx before.
Hi,
I really this is a very nice and useful site. And this feature too. But I realized that this progressbar is very time-consuming, although it is more efficient than the Matlab buit in waitbar. So I was thinking if there is a way to mitigate this problem. Maybe if we don’t use the countdown feature. I don’t know, i didn’t test it. Just a question. What do you guys think about it?
Thanks
Paulo
Very nice, simple. Tnx a lot.
Hi,
What you offer here is very helpful and great, Thank you! Keep up the good work.
I used your progress bar it is good but I wish if I can disable the close button (the X button) in the progress bar window if this is possible, is it possible? because interrupting the execution of my program isn’t a good idea :P, it will have consequences.
Thank you in advance, and thank you a million for this wonderful web site, one of a kind indeed!
Hend