MATLAB Answers

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

14 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Oct 2013
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)
% 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 = '';
% 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, ...
% Starting the timer
% Defining the return value
output = get(hedit,'String');
% Deleting the figure
% Stopping and Deleting the timer

More Answers (0)




Community Treasure Hunt

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

Start Hunting!