1

I need to make a timer that starts counting at the beginning of a multi-phase delay task, and that ends the delay task after a certain period of time has passed, moving on to the next part of the experiment. For now, I'd like to end the task after 2 seconds have passed.

In the code below, I included an example that can be paste into an editor. I used a part of a Stroop task for this delay task that consists of one phase (in my actual code there are 3 phases, but I simplified the task for this question)-- press the 1 key for red, the 2 key for green, and the 3 key for blue. Each phase currently runs for six trials. (just one set of 6 trials for my one phase for now).

I'd like the task itself (all phases together) to last a period of time, and then terminate at the time I set regardless of the trial number. So if the 2 seconds have passed, the task should end even if we are only on phase 1, trial number 3 of 6.

The code below that is commented out (while loop with NumSecondsStart and NumSecondsEnd) is my current attempt. I'm not sure where such a loop would go (around the phase for loop, around the trial loop?) Thanks!

CODE:

clear all

close all

KbName('UnifyKeyNames');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[window, rect]=Screen('OpenWindow',0);

RED=KbName('1'); GREEN=KbName('2'); BLUE=KbName('3');

    keysOfInterest=zeros(1,256);
    keysOfInterest([RED, GREEN, BLUE])=1;
    KbQueueCreate(-1, keysOfInterest);
    KbQueueStart; 

WORDCOLORS = {'red', 'green', 'blue'};

rgbColors = [1000 0 0; 0 1000 0; 0 0 1000];

starttime=Screen('Flip',window);

KbQueueFlush;

% NumSecondsStart = GetSecs;

% while (NumSecondsEnd-NumSecondsStart) == 1

for phase = 1

    design=repmat([1 2 3], 2, 2)';

    if phase == 1

        designRand=design(randperm(length(design)),:);

        Word=WORDCOLORS(designRand(1:6));

        Color=rgbColors(designRand(:,2),:);

    end

    for trial=1:6

        if phase == 1

        DrawFormattedText(window, Word{trial},  'center'  ,'center', Color(trial,:));

         WaitSecs(rand+.5) 

        starttime=Screen('Flip',window); 

        [pressed, firstPress]=KbQueueCheck; 

        endtime=KbQueueWait();  

        RTtext=sprintf('Response Time =%1.2f secs',endtime-starttime); 

        DrawFormattedText(window,RTtext,'center'  ,'center',[255 0 255]); 

        vbl=Screen('Flip',window); 

        Screen('Flip',window,vbl+1); 

        NumSecondsEnd =GetSecs;

        end
    end
end

%                 break;

% end

ListenChar(0); 

ShowCursor(); 
Screen('CloseAll'); 
RTL
  • 3,577
  • 15
  • 24

1 Answers1

0

I think tic and toc will be your friends here...

I would also not use while in favour of the following: (Assuming an iteration which has started will be allowed to finish)

Add tic; where you want the timing to start, not inside the loop or else it will reset the timer each iteration.

Add the following within the for loop, either at the beginning or end

if toc >= 2
    break
end

This will break out of the for loop the first time it reaches this and 2 seconds or more have passed

RTL
  • 3,577
  • 15
  • 24
  • Thanks for responding! Your loop definitely stops the program in my 1 phase case. When I add more phases though, it prevents the user from continuing to the 2nd phase but they can to get all the way through phase 1, extending the entire task for > 2 seconds. Also, it waits for the user response (one can stare at the stimuli for several seconds, respond, and then the task will break). I need the task to stop as soon as 2 seconds is up, break out of phase 1, regardless of the trial or subject response. I’ll continue playing with tic/toc, as it seems more promising than GetSecs. – user3504473 Apr 09 '14 at 00:52
  • ok it seems I didn't grasp the question entirely, there is only one phase in the outer loop in the code given. I suggest taking a look at [`continue`](http://www.mathworks.co.uk/help/matlab/ref/continue.html) instead of break. – RTL Apr 09 '14 at 13:05