Create timing bar with specified duration
21 views (last 30 days)
Show older comments
I'm having real trouble finding any examples of progress bar figures that update to completion over the course of a specified time. How do I do this? I see several functions that seem to estimate the time remaining for a given computation. I need a function where I can just specify some number of seconds over which a progress bar will elapse.
2 Comments
Answers (1)
Simar
on 5 Oct 2023
Edited: Simar
on 9 Oct 2023
Hi Ruaridh Purse,
I understand that you are finding it difficult to find examples of progress bar figure that updates over a specific time. You can use the “waitbar” function in MATLAB. This function allows to display a progress bar that fills up gradually as time passes.
Please follow the below mentioned steps for implementing the “waitbar” function:
- First, specify the total time (in seconds) that the progress bar should take to complete. Let's say in this case it should take 10 seconds to complete.
- Decide how often the progress bar should update. For example, it should update every 0.1 seconds.
- Next, create the progress bar figure using the “waitbar” function. You can give also it a message, such as "Please wait...", and a name for the figure, like "Progress".
- Calculate the number of updates needed by dividing the total time by the update interval. In the example, this would be 10 seconds divided by 0.1 seconds, resulting in 100 updates.
- Use a loop to update the progress bar. Inside the loop, use the waitbar function again to update the progress bar's value. Customize the message to show the progress as a percentage. After each update, pause the loop for the specified update interval using the “pause" function.
- Once the loop is finished, close the progress bar figure using the “close” function.
Please refer to the example code below:
totalTime = 10; % Total time in seconds
updateInterval = 0.1; % Interval at which the progress bar updates (in seconds)
% Create the progress bar figure
h = waitbar(0, 'Please wait...', 'Name', 'Progress');
% Calculate the number of updates needed
numUpdates = totalTime / updateInterval;
% Loop to update the progress bar
for i = 1:numUpdates
waitbar(i/numUpdates, h, sprintf('Progress: %d%%', round(i/numUpdates*100)));
pause(updateInterval); % Pause for the specified update interval
end
% Close the progress bar figure
close(h);
Attaching a Screenshot of progress bar for reference:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1502574/image.png)
Please refer to the documentation links for more details:
- https://www.mathworks.com/help/releases/R2020b/matlab/ref/waitbar.html
- https://in.mathworks.com/help/releases/R2020b/matlab/ref/audioplayer.pause.html?searchHighlight=pause&s_tid=doc_srchtitle
- https://in.mathworks.com/help/releases/R2020b/matlab/ref/timer.stop.html?searchHighlight=stop&s_tid=doc_srchtitle
Best Regards,
Simar
0 Comments
See Also
Categories
Find more on Dialog Boxes in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!