Open or update wait bar dialog box
h = waitbar(x,'message')
A wait bar is a figure that displays what percentage of a calculation is complete as the calculation proceeds by progressively filling a bar with color from left to right.
h = waitbar(x,'message')
displays a wait bar of fractional length
wait bar figure displays until the code that controls it closes it
or the use clicks its Close Window button. Its (figure) handle is
h. The argument
Wait bars are not modal figures (their
CreateCancelBtn adds a Cancel button
to the figure that executes the MATLAB® commands specified in
the user clicks the Cancel button or the Close
waitbar sets both the Cancel button
callback and the figure
CloseRequestFcn to the
string specified in
you to set Figure Properties for
waitbar(x) subsequent calls
waitbar(x) extend the length of the bar to the
x. Successive values of
increase. If they decrease, the wait bar runs in reverse.
waitbar(x,h) extends the
length of the bar in the wait bar
h to the new
updates the message text in the
in addition to setting the fractional length to
Typically, you call
waitbar repeatedly inside
for loop that performs a lengthy computation.
h = waitbar(0,'Please wait...'); steps = 1000; for step = 1:steps % computations take place here waitbar(step / steps) end close(h)
Adding a Cancel button allows user to abort the computation. Clicking it sets a logical flag in the figure's application data (appdata). The function tests for that value within the main loop and exits the loop as soon as the flag has been set. The example iteratively approximates the value of π. At each step, the current value is encoded as a string and displayed in the wait bar's message field. When the function finishes, it destroys the wait bar and returns the current estimate of π and the number of steps it ran.
Copy the following function to a code file and save it as
Execute it as follows, allowing it to run for 10,000 iterations.
[estimated_pi steps] = approxpi(10000)
function [valueofpi step] = approxpi(steps) % Converge on pi in steps iterations, displaying waitbar. % User can click Cancel or close button to exit the loop. % Ten thousand steps yields error of about 0.001 percent. h = waitbar(0,'1','Name','Approximating pi...',... 'CreateCancelBtn',... 'setappdata(gcbf,''canceling'',1)'); setappdata(h,'canceling',0) % Approximate as pi^2/8 = 1 + 1/9 + 1/25 + 1/49 + ... pisqover8 = 1; denom = 3; valueofpi = sqrt(8 * pisqover8); for step = 1:steps % Check for Cancel button press if getappdata(h,'canceling') break end % Report current estimate in the waitbar's message field waitbar(step/steps,h,sprintf('%12.9f',valueofpi)) % Update the estimate pisqover8 = pisqover8 + 1 / (denom * denom); denom = denom + 2; valueofpi = sqrt(8 * pisqover8); end delete(h) % DELETE the waitbar; don't try to CLOSE it.
Nameproperty to describe what is being computed. In the
waitbarsets the fractional progress indicator and displays intermediate results. the code
waitbar(i/steps,h,sprintf('%12.9f',valueofpi))sets the wait bar's
messagevariable to a string representation of the current estimate of pi. Naturally, the extra computation involved makes iterations last longer than they need to, but such feedback can be helpful to users.
You should call
However, as issuing these commands will delete all open figures—not just the wait bar—it is best never to use