How to display a dynamic elapsing time within a message box created by msgbox?
21 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Image Analyst
2017-3-8
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
2020-2-3
编辑:Mathias
2020-2-4
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!