How to display a dynamic elapsing time within a message box created by msgbox?

21 Ansichten (letzte 30 Tage)
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.

Antworten (2)

Image Analyst
Image Analyst am 8 Mär. 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.
  1 Kommentar
Ali Y.
Ali Y. am 10 Mär. 2017
Thank you for your reply and the guide. As you said, I tried to make my own dialog box, but still have some problem with that; as it works fine when I run it outside of a function, but not inside of it. While it is inside the function, the countdown continue like for ever. I will appreciate any help regarding what would be the problem and how can I fix that?
The following lines are those that work properly:
s.fh = figure('NumberTitle','off','Name', 'Elapse time', 'Units','characters',...
'MenuBar','none', 'Toolbar','none', 'Position',[110 40 50 4],...
'Visible','on');
s.tx2 = uicontrol(s.fh,'Style', 'text', 'position', [15 20 100 20],...
'string', 'Elapsed Time:');
s.tx3 = uicontrol(s.fh,'Style', 'text', 'position', [135 20 100 20],...
'string', '');
t = timer;
t.TimerFcn = 'stat=false';
t.Period = 1;
t.StartDelay = period;
t.TasksToExecute = period;
t.ExecutionMode = 'fixedRate';
start(t)
stat=true;
dispTime = period;
while(stat == true)
set(s.tx3,'string',(dispTime))
pause(1)
dispTime = dispTime - 1;
end
stop(t);
delete(t);
close(s.fh)
And, the following lines are within the function I'm talking about.
function etmsgbox (period)
s.fh = figure('NumberTitle','off','Name', 'Elapse time', 'Units','characters',...
'MenuBar','none', 'Toolbar','none', 'Position',[110 40 50 4],...
'Visible','on');
s.tx2 = uicontrol(s.fh,'Style', 'text', 'position', [15 20 100 20],...
'string', 'Elapsed Time:');
s.tx3 = uicontrol(s.fh,'Style', 'text', 'position', [135 20 100 20],...
'string', '');
t = timer;
t.TimerFcn = 'stat=false';
t.Period = 1;
t.StartDelay = period;
t.TasksToExecute = period;
t.ExecutionMode = 'fixedRate';
start(t)
set(s.fh,'deletefcn',{@deleter})
stat=true;
dispTime = period;
while(stat == true)
set(s.tx3,'string',(dispTime))
pause(1)
dispTime = dispTime - 1;
end
close(s.fh)
function deleter(varargin)
stop(t);
delete(t);
end
end

Melden Sie sich an, um zu kommentieren.


Mathias
Mathias am 3 Feb. 2020
Bearbeitet: Mathias am 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

Kategorien

Mehr zu Code Execution finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by