0

I'm having problems with psychtoolbox and accurate timing. I wrote my code following the tutorial on the website and the PTBTutorial-ECVP2013.pdf in PsychDocumentation. But it somehow still doesn't work right. It takes way longer than it should (1 or 2 s) to flip (more than 20 s). So I guess I'm making a critical mistake somewhere, but I can't find it. Could you help me with my code?

In any case this is how my code looks like.

%Number of seconds to wait (ns)
nsS = 2; %sentences
nsW = 1; %words
nsDot = 7;
nsWait= 3;
%Number of frames to wait (wf) before flipping 
wfS = round(nsS/ifi);
wfW = round(nsW/ifi);
wfDot = round(nsDot/ifi);
wfWait = round(nsWait/ifi);
vbl=Screen('Flip', window);


for i = 1:10 %1:exp.ntrials
    sentence = ...; %load sentences
    word = ...% load words;

    for iframe = 1:300
        %draw fixation cross
        if iframe <= 60
            DrawFormattedText(window, '+','center','center', white);
            vbl =Screen('Flip', window, vbl + (wfW-0.5)*ifi);
        %draw sentence
        elseif iframe <= 180
            DrawFormattedText(window, sentence,'center','center', white);
            vbl = Screen('Flip', window, vbl + (wfS-0.5)*ifi);
        %blank screen
        elseif iframe <= 240
            Screen('FillRect', window, black);
            vbl = Screen('Flip', window, vbl + (wfW-0.5)*ifi);
        %Draw word
        elseif iframe <=300
            DrawFormattedText(window, word,'center','center', white);
            vbl = Screen('Flip', window,vbl + (wfW-0.5)*ifi);
        end
    end

    %Draw dot
    for frames = 1:wfDot
        Screen('DrawDots', window, [xCenter yCenter], 10, white, [], 2);
        vbl=Screen('Flip', window, vbl+(wfDot-0.5)*ifi);%, vbl+(wfDot-0.5)*ifi);
        %WaitSecs(7);
    end
    ...
 end  

1 Answers1

0

It looks like you may be using the 'iframe' loop to iterate over screen frames? The Screen('Flip') command will wait to present the stimulus at the appropriate time, the 'iframe' loop is not needed and is what is causing your issue. An example modified from your code is included below (I had to add a few things that weren't defined in your example). Wrapping the code in a try / catch statement isn't necessary, but it's convenient to automatically close the screen on errors.

try
    window = Screen('OpenWindow', 0, 0);
    ifi = Screen('GetFlipInterval', window);

    %Number of seconds to wait (ns)
    nsS = 2; %sentences
    nsW = 1; %words
    nsDot = 7;
    nsWait= 3;
    %Number of frames to wait (wf) before flipping
    wfS = round(nsS/ifi);
    wfW = round(nsW/ifi);
    wfDot = round(nsDot/ifi);
    wfWait = round(nsWait/ifi);
    vbl=Screen('Flip', window);

    black = 0;
    white = 255;

    for i = 1:10 %1:exp.ntrials
        sentence = 'sentence here';
        word = 'word here';

        DrawFormattedText(window, '+','center','center', white);
        vbl =Screen('Flip', window, vbl + (wfW-0.5)*ifi);
        %draw sentence
        DrawFormattedText(window, sentence,'center','center', white);
        vbl = Screen('Flip', window, vbl + (wfS-0.5)*ifi);
        %blank screen
        Screen('FillRect', window, black);
        vbl = Screen('Flip', window, vbl + (wfW-0.5)*ifi);
        %Draw word
        DrawFormattedText(window, word,'center','center', white);
        vbl = Screen('Flip', window,vbl + (wfW-0.5)*ifi);

    end

    sca;

catch
    % close the screen on errors
    sca;
end
DMR
  • 1,479
  • 1
  • 8
  • 11