Hi, I have an experiment wherein subjects must press either the left, right or up arrow button. If they choose another keyboard button, an error message should come up reminding them of the restricted choice and they choose again. If they choose an arrow button then, the rest of the trial continues. However, if they again choose a keyboard button other than an arrow key, the error message appears again and they choose again and so on until they choose correctly. Here is some mock code to demonstrate:
if arrow key pressed
proceed to next part of trial
else
error message
ask for key to be pressed
check key again
return to if statement
end
I am unsure how to code for this since a ‘for’ loop would require a fixed index number to count through, but they could press the wrong key an undefined number of times. Is there a way of going back to a previous point in the code like a ‘flag’ without using a for loop?
Any suggestions would be much appreciated. Thanks in advance!
You decide how many tries you like, or infinite by while loop
while 1 % or for iTry = 1:10
key = getResponse;
if key is valid
record the response
break; % break while loop, go for next trial
else
error message
end
end
Hi, thanks for the answer. I have attempted to implement it as shown:
number_tries = 1;
while number_tries < Inf
if KbName(keyCode1) == "LeftArrow"
elseif KbName(keyCode1) == "RightArrow"
elseif KbName(keyCode1) == "UpArrow"
break;
else
Screen('TextSize', window, 17);
Screen('DrawText', window, 'Please only select an arrow key to indicate your choice', screenXpixels*0.1, screenYpixels*0.5, white);
Screen('Flip', window);
WaitSecs(2.0);
% Question mark
Screen('TextSize', window, 120);
Screen('DrawText', window, '?', screenXpixels*0.48, screenYpixels*0.45, white);
Screen('Flip', window)
% Timestamp
questionmarkOnset1 = GetSecs();
% Check the keyboard to see if a button has been pressed
[secs1, keyCode1] = KbStrokeWait;
number_tries = number_tries + 1;
end
end
However, it keeps crashing when the question mark is displayed when it is supposed to wait for the next keyboard button pressed and then test the ‘if’ condition again.