L.S
L.S

Reputation: 179

Prompt user input, time 4 seconds and prompt again

I have a matrix testNumbers = [1, 3, 8, 6, 9, 7].

What I want to do now is to make the user prompt a input and check if that input is equal to testNumbers(1), if it is do something (for later, %do something) and after 4 seconds continue to make the user input a number again but this time check if testNumbers(2) is equal to the user prompt. This will then continue until the length(testNumbers) has ended.

Can this be done? I assume a for loop has to be used, but I am totally new and therefore a example would be great. Then I can continue building this.

A example:

testNumbers = [1, 3, 8, 6, 9, 7]
  1. A timer starts (4sec)
  2. User inputs 1 within t <= 4 sec
  3. Do something like disp('Correct')
  4. User inputs 3 within t <= 4 sec
  5. Do something like disp('Correct')
  6. User should input 8 but time runs out
  7. Do something like disp('time run out')
  8. User inputs 5 within t <= 4 sec but is wrong, shall be 6
  9. Do something like disp('Wrong')
  10. Continue like this until the matrix ends...

Upvotes: 0

Views: 108

Answers (1)

Suever
Suever

Reputation: 65430

You can use tic and toc to measure the elapsed time between two points. You can place the tic before the user input (to start the timer), and then use a toc wherever you want to check the time that has elapsed since that point. You can use multilple toc's and they will all refer to the closest tic.

% Start the timer
tic

% Prompt the user for input
value = input('Enter a number:');

elapsed_time = toc;

% If the response took more than 4 seconds
if elapsed_time > 4
    disp('took too long')
end

If instead (as your title states) you want to wait 4 seconds, you can use pause to pause execution of your program for a given amount of time

input('Enter a number:');

pause(4)    % Pause for 4 seconds

% Do something else

Upvotes: 3

Related Questions