How can I have a dialog box or user prompt with a time-out period?
24 次查看(过去 30 天)
显示 更早的评论
Can I get code that provides time-out functionality for dialog boxes/input functions such as INPUTDLG, QUESTDLG, INPUT?
This function or dialog that times-out after a given period and returns default answers.
采纳的回答
MathWorks Support Team
2013-2-15
Currently, there is no built-in command in MATLAB that includes a time-out period for the dialog/input prompt.
As a workaround, you can use the TIMER function to close the dialog at time-out. An example is provided below.
function varargout = timeoutDlg(dlg, delay, varargin)
% Dialog function with timeout property
% dlg is a handle to the dialog function to be called
% delay is the length of the delay in seconds
% other input arguments as required by the dialog
% EXAMPLE FUNCTION-CALL
% To display an input dialog box (REFER MATLAB HELP DOC) with a
% timeout = 6 second say, the function call would be:
%
% [matrix_size_value, colormap_string] = timeoutdlg(@inputdlg, 6, ...
% {'Enter matrix size:','Enter colormap name:'}, ...
% 'Input for peaks function', 1, {'20','hsv'})
% Setup a timer to close the dialog in a moment
f1 = findall(0, 'Type', 'figures');
t = timer('TimerFcn', {@closeit f1}, 'StartDelay', delay);
start(t);
% Call the dialog
retvals = dlg(varargin{:});
if numel(retvals) == nargout
varargout = retvals(:);
else
varargout = cell(1, nargout);
end
% Delete the timer
if strcmp(t.Running, 'on')
stop(t);
end
delete(t);
function closeit(src, event, f1)
disp('Time out!');
f2 = findall(0, 'Type', 'figure');
fnew = setdiff(f2, f1);
if ishandle(fnew);
close(fnew);
end
1 个评论
Kouichi C. Nakamura
2017-3-8
编辑:Kouichi C. Nakamura
2017-3-9
Thank you. It worked with brilliantly with questdlg() as well.
function button = questdlgtimeout(delay, varargin)
% questdlg function with timeout property
%
% Based on timeoutDlg by MathWorks Support Team
% https://uk.mathworks.com/matlabcentral/answers/96229-how-can-i-have-a-dialog-box-or-user-prompt-with-a-time-out-period
%
% button = questdlgtimeout(delay,'qstring')
% button = questdlgtimeout(delay,qstring,title)
% button = questdlgtimeout(delay,qstring,title,default)
% button = questdlgtimeout(delay,qstring,title,str1,str2,default)
% button = questdlgtimeout(delay,qstring,title,str1,str2,str3,default)
% button = questdlgtimeout(delay,qstring,title, ..., options)
%
% INPUT ARGUMENTS
% delay Duration in second during withich the dialog is maintained
%
% var1,var2,...
% Accepts input arguments for builtin questdlg. See
% documentation of questdlg
%
% OUTPUT ARGUMENTS
% button The dialog has three default buttons, Yes, No, and Cancel.
% If the user presses one of these three buttons, button is
% set to the name of the button pressed. If the user presses
% the close button on the dialog without making a choice,
% button returns as an empty character vector (''). If the
% user presses the Return key, button returns with a value of
% 'Yes'.
%
% If you provide default or options, button will be the
% default value.
%
%
% See also
% questdlg, timer, scr20170308_154424_questdlgtimeout
%
% Written by Kouichi C. Nakamura Ph.D.
% MRC Brain Network Dynamics Unit
% University of Oxford
% 08-Mar-2017 16:06:58
f1 = findall(0, 'Type', 'figures');
t = timer('TimerFcn', {@closeit f1}, 'StartDelay', delay);
start(t);
dlg = @questdlg;
% Call the dialog
button = dlg(varargin{:});
if isempty(button)
if length(varargin) >= 3
if isstruct(varargin{end})
button = varargin{end}.Default;
elseif ischar(varargin{end})
button = varargin{end};
else
error('unexpected syntax')
end
else % no default provided
% leave button empty
end
end
% Delete the timer
if strcmp(t.Running, 'on')
stop(t);
end
delete(t);
function closeit(src, event, f1)
disp('Time out');
f2 = findall(0, 'Type', 'figure');
fnew = setdiff(f2, f1);
if ishandle(fnew)
close(fnew);
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dialog Boxes 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!