psychtoolbox code help experiment
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I need help with my experiment code. I am a Matlab/pscyhtoolbox beginner and am really struggling with this. I am trying to code a CPT task where letters are displayed on screen quickly. For target letters participants are to press a button - in this case the spacebar - and for non target letters they must inhibit responding.
The below code has got me to the point where I can present instructions a letter but the problem occurs as I cannot figure out how to set a spacebar response as correct for the target letters and no response as correct for the non target letters.
For the no response answers I still want the letters to be presented for a short duration and have errors (so if they press the spacebar for a non target) to be recorded.
I also want to have different blocks where I vary the amount of target to non target letters but I have no idea how to do it and will probably have separate scripts for each block.
Any help or advice will be appreciated. Thanks
Here is my code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % CPT task %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Experimental parameters clear all; rand('state', sum(100*clock)); Screen('Preference', 'SkipSyncTests', 0);
ErrorDelay=.05; interTrialInterval = .05; nTrialsPerBlock = 24;
KbName('UnifyKeyNames'); Key1=KbName('space'); ReturnKey = KbName ('Return'); escKey = KbName('ESCAPE'); corrkey = [32]; % space bar gray = [127 127 127 ]; white = [ 255 255 255]; black = [ 0 0 0]; bgcolor = white; textcolor = black;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Sound feedback BeepFreq = [800 1300 2000]; BeepDur = [.1 .1 .1]; Beep1 = MakeBeep(BeepFreq(1), BeepDur(1)); Beep2 = MakeBeep(BeepFreq(2), BeepDur(2)); Beep3 = MakeBeep(BeepFreq(3), BeepDur(3)); Beep4 = [Beep1 Beep2 Beep3];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Login prompt and open file for writing data out prompt = {'Outputfile', 'Subject''s number:', 'age', 'gender', 'group', 'Num of Blocks'}; defaults = {'NumberExp1', '98', '18', 'F', 'control' , '10'}; answer = inputdlg(prompt, 'NumberExp1', 2, defaults); [output, subid, subage, gender, group, nBlocks] = deal(answer{:}); % all input variables are strings outputname = [output gender subid group subage '.xls']; nblocks = str2num(nBlocks); % convert string to number for subsequent reference
if exist(outputname)==2 % check to avoid overiding an existing file fileproblem = input('That file already exists! Append a .x (1), overwrite (2), or break (3/default)?'); if isempty(fileproblem) | fileproblem==3 return; elseif fileproblem==1 outputname = [outputname '.x']; end end outfile = fopen(outputname,'w'); % open a file for writing data out
fprintf(outfile, 'subid\t subage\t gender\t group\t blockNumber\t trialNumber\t digitShown\t keypressed\t accuracy\t ReactionTime\t \n');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Screen parameters [mainwin, screenrect] = Screen(1, 'OpenWindow'); Screen('FillRect', mainwin, bgcolor); center = [screenrect(3)/2 screenrect(4)/2]; Screen(mainwin, 'Flip');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Experimental instructions, wait for a spacebar response to start Screen('FillRect', mainwin ,bgcolor); Screen('TextSize', mainwin, 24); Screen('DrawText',mainwin,['The following letters are targets : W, K, H, P'], center(1)-300, center(2)-68, textcolor); Screen('DrawText',mainwin,['Your task is to press the spacebar in response to target letters and ignore non-target letters'], center(1)-600, center(2)-1, textcolor); Screen('DrawText',mainwin,['Press enter to start the experiment'], center(1)-280, center(2)+68, textcolor); Screen('Flip',mainwin );
keyIsDown=0; while 1 [keyIsDown, secs, keyCode] = KbCheck; if keyIsDown if keyCode(ReturnKey) break ; elseif keyCode(escKey) ShowCursor; fclose(outfile); Screen('CloseAll'); return; end end end WaitSecs(0.3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Block loop
TargetSetSize = ['W' 'K' 'H' 'P' 'N' 'R' 'X' 'L']
for a = 1:str2num(nBlocks) Screen('FillRect', mainwin, bgcolor); Screen('TextSize', mainwin, 24); Screen('DrawText', mainwin, ['Remember to press the spacebar for the following letters only W K H P'], center(1)-300, center(2)+130, textcolor); Screen('DrawText', mainwin, ['Press enter to start ' num2str(a) ' out of ' nBlocks ' blocks.'], center(1)-300, center(2)+30, textcolor); Screen('Flip', mainwin); KbWait; WaitSecs(1);
trialorder = Shuffle(1:nTrialsPerBlock); % randomize trial order for each block
% trial loop
for i = 1:nTrialsPerBlock;
Letter = TargetSetSize(mod(trialorder(i),8)+1); % array index 1-8, corresponds to 1-4, 6-9
Screen('FillRect', mainwin ,bgcolor);
Screen('FillRect', mainwin ,bgcolor);
Screen('TextSize', mainwin, 60);
Screen('DrawText', mainwin, num2str(Letter), center(1)-30, center(2)-30, textcolor);
Screen('Flip', mainwin); % must flip for the stimulus to show up on the mainwin
%ShowCursor('hand');
% now record response
timeStart = GetSecs;keyIsDown=0; correct=0; rt=0;
while 1
[keyIsDown, secs, keyCode] = KbCheck;
FlushEvents('keyDown');
if keyIsDown
nKeys = sum(keyCode);
if nKeys==1
if keyCode(Key1)||keyCode(Key2)
rt = 1000.*(GetSecs-timeStart);
keypressed=find(keyCode);
Screen('Flip', mainwin);
break;
elseif keyCode(escKey)
ShowCursor; fclose(outfile); Screen('CloseAll'); return
end
keyIsDown=0; keyCode=0;
end
end
end
if (keypressed==corrkey(1)&&Letter<5)||(keypressed==corrkey(2)&&Letter>5)
correct=1;Snd('Play', Beep4);
else
correct=0; Snd('Play', Beep1); WaitSecs(ErrorDelay);
end
Screen('FillRect', mainwin ,bgcolor); Screen('Flip', mainwin);
% write data out
fprintf(outfile, '%s\t %s\t %s\t %s\t %d\t %d\t %d\t %d\t %d\t %6.2f\t \n', subid, ...,
subage, gender, group, a, i, Letter, keypressed, correct, rt);
WaitSecs(interTrialInterval);
end % end of trial loop
end % end of block loop
Screen('CloseAll'); fclose(outfile); fprintf('\n\n\n\n\nFINISHED this part! PLEASE GET THE EXPERIMENTER...\n\n');
1 Kommentar
RBealPsych
am 10 Jan. 2017
I'm having the same problem myself with an oddball task paradigm, where the correct response is sometimes no response at all. Did you manage to resolve this, and if yes how so?
Antworten (0)
Siehe auch
Kategorien
Mehr zu Psychtoolbox-3: Vision and neuroscience research finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!