How can I implement a timer dependent input request in MATLAB?

8 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日
I would like to interactively request an input from the user of my application, but unlike INPUT I want to limit the time for giving the answer and automatically continue the program if nothing is inserted.

採用された回答

MathWorks Support Team
MathWorks Support Team 2013 年 10 月 18 日
This kind of input request is not possible using INPUT or other functions in MATLAB 7.5 (R2007b).
To work around this issue, the following function may be used:
function output = timeinput(t,default_string)
% TIMEINPUT
% Input arguments:
% t - time delay
% default_string - string which is returned if nothing is entered
%
% Examples:
% If a string is expected
% x = timeinput(20,'no input')
% If a number is expected
% x = str2num(timeinput(20,'1'))
%
if nargin == 1
default_string = '';
end
% Creating a figure
h = figure('CloseRequestFcn','','Position',[500 500 200 50],'MenuBar','none',...
'NumberTitle','off','Name','Please insert...');
% Creating an Edit field
hedit = uicontrol('style','edit','Units','pixels','Position',[10 15 180 20],'callback','uiresume','string',default_string);
% Defining a Timer object
T = timer('Name','mm', ...
'TimerFcn','uiresume', ...
'StartDelay',t, ...
'ExecutionMode','singleShot');
% Starting the timer
start(T)
uiwait(h)
% Defining the return value
output = get(hedit,'String');
% Deleting the figure
delete(h)
% Stopping and Deleting the timer
stop(T)
delete(T)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInstall Products についてさらに検索

製品


リリース

R2006b

Community Treasure Hunt

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

Start Hunting!

Translated by