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

조회 수: 20 (최근 30일)
Ali Y.
Ali Y. 2017년 3월 8일
편집: Mathias 2020년 2월 4일
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.

답변 (2개)

Image Analyst
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.
  댓글 수: 1
Ali Y.
Ali Y. 2017년 3월 10일
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

댓글을 달려면 로그인하십시오.


Mathias
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

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by