L.S
L.S

Reputation: 179

GUI wait for user input and do something if time runs out

I am working with a script that takes user input. I want the user to answer on the input within 4 seconds otherwise the code shall do something (right now without using a GUI). What I have done so far is to start a tic and prompt a input testInput = input('Enter the number: '); Meanwhile the prompt is open (waiting for input) I am checking if time has not runed out:

elapsed_time = toc; if elapsed_time > 4 %do something end

To the problem: From what I have learned, there's no way to programmatically terminate a running command in MATLAB, or even let the code do something while the prompt is open. Therefore I can not check if 4 sec has passed before the user inputs something. I've read (here) that this might be possible to solve by using a GUI. I have although no idea how to implement this, since I am totally new.

So, would this be possible with a GUI? Because with the command window it is not.

I would really appreciate to see how something simple like this could look like as a GUI (just something very simple, a window with a input box):

%Start a time ticker
tic;

testInput = input('Enter the number: ');

elapsed_time = toc;

 if elapsed_time > 4
    %do something
 end

Upvotes: 1

Views: 403

Answers (1)

NLindros
NLindros

Reputation: 1693

Here is a small example of a custom GUI where the user should enter a number before a maximum time is reached. Please read some about callbacks, handles, figure options and uicontrol to better understand it.

Note that you might need to do som more fault handling of the input string (check that number is valid)

function EnterNumber()

% Create figure
inDlg = figure('NumberTitle','off','MenuBar','none','ToolBar','none');

% Create timer, set it to run TimerFcn after 4 s
maxTime = 4;
tH = timer('TimerFcn', {@closeFig inDlg}, 'StartDelay', maxTime);

% Create text and input box for number in figure
uicontrol(inDlg,...
    'Style','Text','FontSize',14, ...
    'String',sprintf('Please enter a number within %d seconds:', maxTime),...
    'Units','Normalized','Position',[0.1 0.6 0.8 0.2]);
editBox = uicontrol(inDlg,...
    'Style','Edit','Units','Normalized','Position',[0.1 0.5 0.8 0.2], ...
    'FontSize',14,'Callback',@returnEditValue);

% Start timer
start(tH);

% Set focus on the edit box so a number could be entered instantly
uicontrol(editBox)

% Wait for figure to be closed (in any way)
waitfor(inDlg)
fprintf('Moving on ...\n');


% Callback function to save number
function returnEditValue(hObject,~)
% Get the number
number = str2double(get(hObject,'String'));
% Example of how to display the number
fprintf('The entered number is %d\n', number);
% Example of saving the number to workspace
assignin('base','number', number); 
% Close figure
close(get(hObject,'Parent')); 


% Calback function for timer timeout
function closeFig(~,~,figH)
% If figure exist, close it
if ishandle(figH)
    close(figH)
    fprintf('Too slow!\n')
end

Upvotes: 1

Related Questions