% StroopStimResponse.m % % This program presents stroop like stimuli and record keypress response. % Written by YW, 2022 try clear all close all subid='3'; subname='wangwu'; % predefine parameters BlockNum=1;% how may block in this experiment StimDuration=2000;% 2000ms ISImin=1000;% the minmum interstimulus interval (ISI) ISImax=1500;% the maxmum interstimulus interval (ISI) TxtSize=100;% the size of the texts TxtFont='Arial'; % the font of the texts Words={'Red', 'Green', 'Blue'}; StimColor={[255 0 0];[0 255 0];[0 0 255]}; count=0; for i=1:length(Words) for j=1:length(StimColor) count=count+1; Stim(count).word=Words{i}; % stimulus word Stim(count).color=StimColor{j};% stimulus color end end % to preset variables 'results' for recording data results.word = NaN*ones(BlockNum,length(Stim)); results.color = NaN*ones(BlockNum,length(Stim)); results.response = NaN*ones(BlockNum,length(Stim)); results.reactiontime = NaN*ones(BlockNum,length(Stim)); % open window screenNum=0; [wPtr,rect]=Screen('OpenWindow',screenNum); HideCursor; black=BlackIndex(wPtr); white=WhiteIndex(wPtr); monitorFlipInterval=Screen('GetFlipInterval', wPtr); hz=FrameRate(wPtr); StimDuration=round((StimDuration/1000)*hz); % the number of frames for stimulusduration ISIrange=round(((ISImax-ISImin)/1000)*hz); ISImin=round((ISImin/1000)*hz); ISI=round(rand(BlockNum,length(Stim))*ISIrange+ISImin);% the number of frames for neach trial's ISI in a range of ISImin and ISImax % blank the Screen starttime=Screen('Flip',wPtr); % collect the time for the first flip with starttime for ii=1:BlockNum Screen('TextSize', wPtr , TxtSize); Screen( 'TextFont', wPtr ,TxtFont); StimRand=Stim(randperm(length(Stim))); % randomize all stimuli for jj=1:length(Stim) TxtRect= Screen('TextBounds',wPtr,StimRand(jj).word); % get the size of each stimulus text TxtLoc=[round(rect(3)/2-TxtRect(3)/2),round(rect(4)/2-TxtRect(4)/2)]; % compute the upperleft and lowerright location of each stimulus text, to make the text in the middle of the screen Screen('DrawText', wPtr, StimRand(jj).word, TxtLoc(1), TxtLoc(2),StimRand(jj).color); starttime=Screen(wPtr, 'Flip', starttime+(ISI(ii,jj)*monitorFlipInterval)); % time in secs [keys,RT] = waitTilltime(StimDuration*monitorFlipInterval,starttime);%time in secs starttime=Screen('Flip', wPtr);% time in secs %%% save keypress response if strcmp(StimRand(jj).word,Words{1})==1 results.word(ii,jj)=1; % word1 'Red' elseif strcmp(StimRand(jj).word,Words{2})==1 results.word(ii,jj)=2;% word2 'Green' elseif strcmp(StimRand(jj).word,Words{3})==1 results.word(ii,jj)=3;% word3 'Blue' end results.color(ii,jj)=find(StimRand(jj).color~=0); % color 1=red,2=green, 3=blue if ~isempty(RT) results.reactiontime(ii,jj)=RT; results.key{ii,jj}=keys{1}; if (results.key{ii,jj}=='f' && results.color(ii,jj)==1)... ||(results.key{ii,jj}=='j' && results.color(ii,jj)==2)... ||(results.key{ii,jj}=='k' && results.color(ii,jj)==3) results.response(ii,jj) = 1;% f for color red, j for color green, k for color blue are correct else results.response(ii,jj) = 0;% incorrect end end end end ISIlast=round(rand(1)*ISIrange+ISImin); % the last trial's ISI % blank the screen and wait a second Screen('FillRect',wPtr,white); Screen(wPtr, 'Flip', starttime+(ISIlast*monitorFlipInterval)); % save the datafile to disc for each participant datafile=['StroopData_Sub' subid '_' subname]; path=pwd; save ([path '\Data\' datafile '.mat'], 'results'); % save as mat file Screen('CloseAll'); ShowCursor catch Screen('CloseAll') rethrow(lasterror) end