How to implement an trial - timeout for reaction time task?

6 views (last 30 days)
I have set up a the trial for an reaction task. Right now the next trial only starts if a key has been pressed otherwise the current screen just stays as it is. I want to implement the trial run in a way, that after 2 seconds the next trial starts, no matter whether an response (key press) has been made or not.
This is the current code (without the timeout of 2 secs:
% Wait for response and present background
while down == 0
[down , ~, key] = KbCheck;
if down == 1
break;
end
end
if strcmp(KbName(key),'q') % escape key
ListenChar;
Screen('CloseAll');
ShowCursor;
else
keyCode = KbName(key);
response = KbName(keyCode);
response=response(1);
end
% now classify response
responseKey = find(rkeys == response); %translates ASCII code
if isempty(responseKey)
whichKey(trial) = 9;
else
if responseKey == 1
whichKey(trial) = 1;
elseif responseKey == 2
whichKey(trial) = 2;
end
end
I have thought of a solution fo the timeout like this, but it does not work, because suddenly it does not recognize the variable key anymore
while down == 0 && vbl <= 2
[down , ~, key] = KbCheck;
if down == 1 %or alternatively I thought of instead putting vbl <=2 above to put vbl > 2 here
break;
%%% get the right keys or else it will also stay as it was
end
end
if strcmp(KbName(key),'q') % escape key
ListenChar;
Screen('CloseAll');
ShowCursor;
else
keyCode = KbName(key);
response = KbName(keyCode);
response=response(1);
end
% now classify response
responseKey = find(rkeys == response); %translates ASCII code
if isempty(responseKey)
whichKey(trial) = 9;
else
if responseKey == 1
whichKey(trial) = 1;
elseif responseKey == 2
whichKey(trial) = 2;
end
end
Here is the full code from the beginning so far, if anybody has to need it for understanding my question above:
% Clear the workspace and the screen
sca;
close all;
clearvars;
fmask = 'KSNIENFAPENGTZRR';
prime = 'KÖRPERFETTANTEIL';
bmask = 'WKVOEMGJTHOSPFRL';
wordleft = 'glücklich';
wordright = 'verzweifelt';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set preferences
Screen('Preference', 'SkipSyncTests', 1);
Screen('Preference', 'VisualDebugLevel', 3);
% search for screens
screenNumber = max(Screen('Screens'));
% open white screen
white = 255;
black = 0;
textColor = [0 0 0];
[mainWindow,windowRect] = Screen('OpenWindow', screenNumber, white);
Screen('BlendFunction', mainWindow, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
% maximum priority for accurate timing
Priority(MaxPriority(mainWindow));
% set shuffle to new random number generator
rng('shuffle');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get the size of the on screen window
[screenXpixels, screenYpixels] = Screen('WindowSize', mainWindow);
% Query the frame duration
ifi = Screen('GetFlipInterval', mainWindow);
% Setup the text type for the window
Screen('TextFont', mainWindow, 'Ariel');
Screen('TextSize', mainWindow, 30);
% Get the centre coordinate of the window
[xCenter, yCenter] = RectCenter(windowRect);
%word position:
xleft = xCenter-400; %left of fixation cross
xright = xCenter+300; %right of fixation cross
% Bestimmen der Präsentationszeiten
slack = Screen('GetFlipInterval',mainWindow)/2;
% fixDur = max([(0.75 - slack) 0]); % Hintergrund für 800 ms
% fmaskDur = max([(0.2 - slack) 0]); % Rechteck für 1200 ms
% primeDur = max([(0.02 - slack) 0]);
% bmaskDur = max([(0.03 - slack) 0]);
fixDur = 0.75 - slack; % Hintergrund für 800 ms
fmaskDur = 0.2 - slack; % Rechteck für 1200 ms
primeDur = 0.02 - slack;
bmaskDur = 0.03 - slack;
% Here we set the size of the arms of our fixation cross
fixCrossDimPix = 40;
% Now we set the coordinates (these are all relative to zero we will let
% the drawing routine center the cross in the center of our monitor for us)
xCoords = [-fixCrossDimPix fixCrossDimPix 0 0];
yCoords = [0 0 -fixCrossDimPix fixCrossDimPix];
allCoords = [xCoords; yCoords];
% Set the line width for our fixation cross
lineWidthPix = 4;
trialNumber = 5;
RTMatrix = zeros(1,trialNumber);
%Define response keys
KbName('UnifyKeyNames') ;
leftKey = KbName('LeftArrow') ;
rightKey = KbName('RightArrow') ;
continuing = KbName ('Space') ;
escape = KbName ('Escape') ;
whichKey = zeros(trialNumber, 1);
rkeys = [leftKey, rightKey]; %responsekeys
ListenChar(2); % This suppresses output of keypresses
HideCursor; % This hides the cursor.
vbl = GetSecs;
for trial = 1:trialNumber
% Draw the fixation cross in white, set it to the center of our screen and
% set good quality antialiasing
Screen('DrawLines', mainWindow, allCoords, lineWidthPix, black, [xCenter yCenter], 2);
% Flip to the screen
vbl = Screen('Flip', mainWindow); % Hintergrund sofort präsentieren
% Wait for a key press
%KbStrokeWait;
DrawFormattedText(mainWindow,uint8(fmask),'center','center',textColor,[],[],[]);
Screen('Flip', mainWindow);
vbl = Screen('Flip', mainWindow, vbl + fixDur);
DrawFormattedText(mainWindow,uint8(prime),'center','center',textColor,[],[],[]);
vbl = Screen('Flip', mainWindow, vbl + fmaskDur);
DrawFormattedText(mainWindow,uint8(bmask),'center','center',textColor,[],[],[]);
vbl = Screen('Flip', mainWindow, vbl + primeDur);
% Draw the fixation cross in white, set it to the center of our screen and
% set good quality antialiasing
Screen('DrawLines', mainWindow, allCoords,...
lineWidthPix, black, [xCenter yCenter], 2);
% 3. Schritt: Text zeichnen
DrawFormattedText(mainWindow,uint8(wordleft), xleft,'center', black);
DrawFormattedText(mainWindow,uint8(wordright),xright,'center', black);
vbl = Screen('Flip', mainWindow, vbl + bmaskDur);
down = 0;
% Wait for response and present background
while down == 0
[down , ~, key] = KbCheck;
if down == 1
break;
end
end
if strcmp(KbName(key),'q') % escape key
ListenChar;
Screen('CloseAll');
ShowCursor;
else
keyCode = KbName(key);
response = KbName(keyCode);
response=response(1);
end
% now classify response
responseKey = find(rkeys == response); %translates ASCII code
if isempty(responseKey)
whichKey(trial) = 9;
else
if responseKey == 1
whichKey(trial) = 1;
elseif responseKey == 2
whichKey(trial) = 2;
end
end
end
%KbStrokeWait;
% Clear the screen
% Close Screen
Screen('CloseAll')
% Clean up
ListenChar(0);
Priority(0);
ShowCursor;
fclose all;
clear;
  1 Comment
dpb
dpb on 16 Aug 2021
Edited: dpb on 16 Aug 2021
Are you using some or a set of Toolboxes -- most of the functions called in your code aren't in base MATLAB nor in your posted code so no klew how they work...
As for just a guess, there are timers built into MATLAB, you might try setting one of those to generate an interrupt when the timeout period has passed. Alternatively, you could just monitor system clock in the tight loop and break if elapsed time exceeds the timeout.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!