0

I am trying to record participant responses to visual stimuli which need to be shown for a fixed duration (500ms). However, I would also like to record their responses after this fixed duration within a 1 sec time window from stimulus onset. The following code allows me to record responses only when a key is pressed during the presentation of the stimulus (500ms).

    StimulusSecs = 0.50001;
    StimDuration = round(StimulusSecs / ifi);
    ISISecs = 0.50001;
    ISI = round(ISISecs / ifi);
    ITISecs = [1.00001, 2.00001, 3.00001];
    intertrial = round(ITISecs / ifi);
    waitframes = 1;
    vbl = Screen('Flip', mainwin);
    t2wait = 1;

    [~, Onset]=Screen('Flip', mainwin); keyIsDown=0; rt=0;

    tic % Stimulus Onset

    for frame = 1:StimDuration
    Screen('DrawTexture', mainwin, pics(picCounter));
    vbl = Screen('Flip',mainwin, vbl + (waitframes - 0.5) * ifi);
    [keyIsDown, secs, keyCode] = KbCheck;
    FlushEvents('keyDown');
    if keyIsDown
        if keyCode(spaceKey)
            rt = 1000.*(secs-Onset);
            keypressed=find(keyCode); 
            % break; is missing as this would 
            % escape the loop
        elseif (secs-Onset) > t2wait
            break;
        elseif keyCode(escKey)
            ShowCursor; fclose(outfile); Screen('CloseAll');
            return;
        end
        keyIsDown=0; keyCode=0; keypressed=0;
    end
end
S2dur = toc;

I am new to psychoolbox and any help would be greatly appreciated!!!

Marco
  • 3
  • 1

2 Answers2

0

I am assuming that picCounter remains constant for the whole 1 sec time of a given trial. I think you could achieve what you need by changing the loop so that it goes on querying the keyboard for 1 sec, but adding a condition so that what is displayed depends on how much time has elapsed.

start_time = GetSecs; % stimulus onset
while GetSecs-start_time<1

    % draw stimulus only if elapsed time is < 0.5 sec
    if GetSecs-start_time<0.5
        Screen('DrawTexture', mainwin, pics(picCounter));
    end
    vbl = Screen('Flip',mainwin, vbl + (waitframes - 0.5) * ifi);

    % proceed by querying keyboard, etc.
    [keyIsDown, secs, keyCode] = KbCheck;

    (...)

end
matteo
  • 311
  • 1
  • 9
0

You're currently re-drawing and re-flipping the stimulus for every frame. If these are static images, you don't need to do that, as you only need to re-draw and flip when you want what is on screen to change. Here is an example assuming a static image. The key response loop operates over the whole t2wait period. Within that loop, a conditional statement compares if time allotted for the stimulus has elapsed, and if so, remove it from the screen:

StimulusSecs = 0.50001;
StimDuration = round(StimulusSecs / ifi);
ISISecs = 0.50001;
ISI = round(ISISecs / ifi);
ITISecs = [1.00001, 2.00001, 3.00001];
intertrial = round(ITISecs / ifi);
waitframes = 1;
vbl = Screen('Flip', mainwin);
t2wait = 1;

tic % Stimulus Onset

[~, Onset] = Screen('DrawTexture', mainwin, pics(picCounter));
OnScreen = 1; % variable to indicate whether the stim is still onscreen
keyPressed = 0; % variable will be 0 until the space key is pressed
rt=0;
while (GetSecs - Onset) <=  t2wait

    [keyIsDown, secs, keyCode] = KbCheck;

    if keyPressed == 0 && keyIsDown
        if keyCode(spaceKey)
            rt = 1000.*(secs-Onset);
            keypressed=find(keyCode);
        elseif keyCode(escKey)
            ShowCursor; fclose(outfile); Screen('CloseAll');
            return;
        end
    end

    % turn the stimulus off if StimulusSecs has elapsed
    if OnScreen == 1 && ((GetSecs - Onset) >= StimulusSecs)
        Screen('Flip',wPtr);
        OnScreen = 0;
    end

    % Wait 1 ms before checking  again to prevent
    % overload of the machine at elevated priority
    WaitSecs(0.001);
end
DMR
  • 1,479
  • 1
  • 8
  • 11
  • Dear DMR. Yes I am using static images and your suggestion works great! Thank you so much. Best wishes – Marco Jan 13 '18 at 15:08