How to display a dynamic elapsing time within a message box created by msgbox?
21 views (last 30 days)
Show older comments
Hi, I have a message box that is created by msgbox and it is closed after 3 seconds. Is there a way to show the elapsed time, dynamically in one second intervals? Say the code is like
tic
h1 = msgbox({'This message box will be closed in 3 seconds.' '' num2str(toc)} ,'Info');
uiwait(h1,3)
close (h1)
I have to say that the solution that exist in the following link doesn't work.
0 Comments
Answers (2)
Image Analyst
on 8 Mar 2017
msgbox() takes a static text. To get a dialog box where the elapsed time is continuously updated on the window, you'll have to create your own dialog box with a static text label. Periodically, like every second or whatever, you have to create a new string with sprintf() and send it to the label and call drawnow:
elapsedTime = toc(startTime); % Or however you compute it....
str = sprintf('Elapsed time = %f seconds.', elapsedTime);
handles.txtTimeLabel.String = str; % Send string to the text control on the GUI
drawnow; % Force immediate update/refresh of the GUI.
Mathias
on 3 Feb 2020
Edited: Mathias
on 4 Feb 2020
function tmsgbox(inStr, tlim, varargin)
% message box with close timer
% inStr = 'This is still for testing';
% tlim = 3; % seconds
if ischar(inStr)
inStr = {inStr};
end
tdiff = 0;
t = msgbox([inStr; {sprintf('%.1f sec.', tlim-tdiff)}], varargin{:});
tstart = now; % in days
while isgraphics(t) && tdiff < tlim
% Children(2) = axes
% Children(2).Children(1) = text in axes
t.Children(2).Children(1).String = [inStr; {sprintf('%.1f sec.', tlim-tdiff)}];
pause(0.2);
tdiff = (now-tstart)*3600*24; % days to seconds
end
if isgraphics(t)
% delete if timer is at zero
delete(t);
end
isgraphics() fails if user delete the box by click.
Edit (04.02.2020): as function to replace msgbox
0 Comments
See Also
Categories
Find more on Code Execution 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!