# If you're using tic, toc, and var=input('prompt'), is there a way for toc to interrupt the user input if they take too long to answer?

9 ビュー (過去 30 日間)
Christina Diersing 2021 年 7 月 28 日
コメント済み: Cam Salzberger 2021 年 7 月 30 日
Hi, I'm working on a project where I have to time people's answers for math problems. Currently, I'm working on 1 problem where they have up to 20 seconds to answer. Is there a way to have Matlab interrupt and tell the user they took too long to answer? So, if they don't answer in 20 seconds, a line will pop up telling them they took too long?
This is the code I have so far for it, but it won't tell the user they took too long until after they input an answer.
close all; clear all; clc;
fprintf('Directions: Complete the following Math problems in your head, then type the answer on the number pad. You will have up to 1 minute to answer each question. Please press any button to continue.\n\n')
%pause;
tstart=tic;
teass=tic;
fprintf('Start teass')
while toc(teass)<20
easAns=input(' 76 \n x10 \n ')
if toc(teass) >= 20
teasans=toc(teass)
easAns=0;
fprintf('Out of time');
elseif easAns==ECorAns
fprintf(' \n Correct! Good Job! \n');
teasans=toc(teass)
else
fprintf('Incorrect');
teasans=toc(teass)
end
end
tend=toc(tstart)
fprintf('\n End \n')
I think I have it set so the code will remember teass once the code has completely run, so I can record the data.
Thank you!

サインインしてコメントする。

### 採用された回答

Cam Salzberger 2021 年 7 月 28 日
Hello Christina,
You could try using an inputdlg to ask the question, instead of keeping it in the command window. You could get the handle to the dialog box and close it if they take too long.
For a little more customization, you could create your own figure or uifigure pop-up to ask the question.
-Cam
##### 2 件のコメント表示非表示 1 件の古いコメント
Cam Salzberger 2021 年 7 月 30 日
Hmm, I did a little testing with inputdlg, and could make a neat little quiz:
answers = inputdlg(["1+1 = ?", "2+2 = ?"], "Math Problem", 1, ["", ""], struct("WindowStyle", "modal", "Resize", "on"))
Unfortunately, it automatically blocks until the window closes. So you would need to make your own dialog, figure, or uifigure window. This will give you the handle to the figure, and you can close it or change its state if they take too long, based on a tic-toc if you want, but a timer would make more sense.
You'll need to add the same text, edit and pushbutton uicontrols (for dialog or figure) or uilabel, uieditfield, and uibutton (for uifigure) to mimic the dialog. Then you add a callback to the button to retrieve the answers and check them or return them somewhere for you to process.
It's a lot more involved, but all GUI creation is. App Designer would probably help you do this a lot faster, as it provides the skeleton of the code for you, and allows for drag-and-drop of components.
-Cam

サインインしてコメントする。

### その他の回答 (2 件)

TADA 2021 年 7 月 28 日

Its possible, not using toc though.
The problem is this, once your code reaches the input function it freezes in that line and waits for keyboard interaction.
the next line of code is only run after input is returned so toc is not reached at all.
you can use a timer and then proggramatically type the enter key on the "keyboard" using JAVA
timeout = timer();
timeout.ExecutionMode = 'singleShot';
timeout.TimerFcn = @doTimeout;
timeout.StartDelay = 10;
timeout.start();
s = input('type something:', 's');
function doTimeout(tmr, ea)
import java.awt.Robot;
import java.awt.event.KeyEvent;
robot=Robot;
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
end
This small example shows how to stop the input function forcefully after 10 seconds using the trick I mentioned.
This could backfire though if the user starts typing then the timer fires and stops the user in the middle.
##### 0 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

Walter Roberson 2021 年 7 月 28 日
I discuss a list of other possibilities provided by the third party Psychtoolbox

サインインしてコメントする。

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by