Abort during waitsecs

Hi all,

I want to use DrawFormattedText like below to display text for an fMRI experiment.

DrawFormattedText(window, 'text', 'center', 'center', [255 255 255]);
Screen('Flip', window);
WaitSecs(120);

DrawFormattedText(window, 'text', 'center', 'center', [255 255 255]);
Screen('Flip', window);
WaitSecs(240);

when we use waitsecs to display a text for a few minutes, sometime, we will get situations where we need to abort the execution of the screen. So as waitsecs can’t be killed, is there any other way to do this except waitsecs?

Please advise.
Thank you.

Best regards,
Ram

Just put this in a while loop and use KbCheck or something to allow you to exit the loop, something like this pseudo-code:

vbl = Screen('Flip',window); tStart = vbl;
while vbl <= tStart + 120;
    DrawFormattedText(window, ...);
    vbl = Screen('Flip', window);
    if any(KbCheck(-1)); break; end
end

It would be better to check for specific key presses, you can use RestrictKeysForKbCheck for more specificity or use the keyCode return value and inspect that…

1 Like

Thank you very much for this. It works well.