kkauraiims / testrepo

0 stars 0 forks source link

stroop_kk_2 #2

Open kkauraiims opened 7 months ago

kkauraiims commented 7 months ago

%% This is code conducts stroop task in psychtoolbox % KK- March, 2024 % revised- April, 2024

%------------------------------------------------------------------------- % Preliminary Set-up %------------------------------------------------------------------------- % Clear the workspace close all clearvars sca;

%------------------------------------------------------------------------- % Build a GUI to get subject number %------------------------------------------------------------------------- prompt = {'Subject Number'}; % what information do we want from the subject? box = inputdlg(prompt,'Enter Subject Info'); % build the GUI

p.date_time_start = clock; % record time and date of the start of the sesssion !!

if length(box) == 1 % check to make sure something was typed in p.subNum = str2num(box{1}); else return; % if nothing was entered or the subject hit cancel, bail out end

%------------------------------------------------------------------------- % Build an output directory & check to make sure it doesn't already exist %------------------------------------------------------------------------- p.root = pwd; % make an output dir for the subject mkdir(strcat(p.root,'/Subject_Data',filesep,box{1}));

%------------------------------------------------------------------------- % Set-up Psychtoolbox %-------------------------------------------------------------------------

% Setup PTB with some default values PsychDefaultSetup(2);

% Seed the random number generator rng('shuffle')

% Set the screen number to the external secondary monitor if there is one % connected screenNumber = max(Screen('Screens'));

% Define black, white and grey white = WhiteIndex(screenNumber); grey = white / 2; black = BlackIndex(screenNumber);

% Open the screen [window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey, [], 32, 2);

% Flip to clear Screen('Flip', window);

% Query the frame duration ifi = Screen('GetFlipInterval', window);

% Set the text size Screen('TextSize', window, 60);

% Get the centre coordinate of the window [xCenter, yCenter] = RectCenter(windowRect);

% Set the blend funciton for the screen Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');

% Set maximum priority level topPriorityLevel = MaxPriority(window); Priority(topPriorityLevel);

%---------------------------------------------------------------------- % Timing Information %----------------------------------------------------------------------

% Interstimulus interval time in seconds and frames isiTimeSecs = 1; isiTimeFrames = round(isiTimeSecs / ifi);

% Numer of frames to wait before re-drawing waitframes = 1;

%---------------------------------------------------------------------- % Keyboard information %----------------------------------------------------------------------

% Define the keyboard keys that are listened for. We will be using the left % and right arrow keys as response keys for the task and the escape key as % a exit/reset key escapeKey = KbName('ESCAPE'); leftKey = KbName('LeftArrow'); rightKey = KbName('RightArrow'); downKey = KbName('DownArrow');

%---------------------------------------------------------------------- % Colors in words and RGB %----------------------------------------------------------------------

% We are going to use three colors for this demo. Red, Green and blue. colorList = ['red';'green';'blue']; rgbColors = [1 0 0; 0 1 0; 0 0 1];

colortable = table(colorList, rgbColors);

%---------------------------------------------------------------------- % Import the condition matrices %----------------------------------------------------------------------

stroop_condMatrix_pract = readtable ('/Users/neelbazro/Desktop/BBSRC_project/psych_scripts/StroopTrialTypes2.xlsx', 'ReadVariableNames', true); stroop_condMatrix_1 = readtable ('/Users/neelbazro/Desktop/BBSRC_project/psych_scripts/StroopTrialTypesA.xlsx', 'ReadVariableNames', true); stroop_condMatrix_2 = readtable ('/Users/neelbazro/Desktop/BBSRC_project/psych_scripts/StroopTrialTypesB.xlsx', 'ReadVariableNames', true); stroop_condMatrix_3 = readtable ('/Users/neelbazro/Desktop/BBSRC_project/psych_scripts/StroopTrialTypesC.xlsx', 'ReadVariableNames', true); stroop_condMatrix_4 = readtable ('/Users/neelbazro/Desktop/BBSRC_project/psych_scripts/StroopTrialTypesD.xlsx', 'ReadVariableNames', true);

% create a structure from the condition matrices condMatrix(1).data = stroop_condMatrix_1; condMatrix(2).data = stroop_condMatrix_2; condMatrix(3).data = stroop_condMatrix_3; condMatrix(4).data = stroop_condMatrix_4;

%---------------------------------------------------------------------- % Make a master condition and response matrix %----------------------------------------------------------------------

% Define the number of trials % this trial contains four blocks of each condition matrix type numTrials = height(condMatrix(1).data);

% set the number of blocks, in this experiment they were set to 4 numBlocks = 4;

% This is a ... row matrix % the first row will record the word we present, % the second row the color of the word it is written in, % the third row the key participant respond with % fourth row is the time they took to make there response. % The fifth row is the presentation time of the ISI % 6th row is the time stmap- start time of the trial % 7th row is the time stamp- End of ISI interval, start of the cue presentation time % 8th row is the time stamp- time of key press by the participant

for n= 1: numBlocks respMat_stroop(n).data = cell(8, numTrials); end

%---------------------------------------------------------------------- % Experimental loop %----------------------------------------------------------------------

% begin Block Loop for b= 1: numBlocks

% extract conditions table for the given block 
condMatrix_block = condMatrix(b).data;

% begin trial loop for t = 1:numTrials

% The color word and the color it is drawn in
theWord = condMatrix_block{t,1}; 
theColor_word = condMatrix_block{t,2};

% find the corresponding rgb value for the color word 

color_idx = find(colortable{:,1}== string(theColor_word));
theColor = colortable{color_idx,2};

% Cue to determine whether a response has been made
respToBeMade = true;

% If this is the first trial we present a start screen and wait for a
% key-press
if t == 1
    DrawFormattedText(window, 'Name the color \n\n Press Any Key To Begin',...
        'center', 'center', black);
    Screen('Flip', window);
    KbStrokeWait;
end

% Flip again to sync us to the vertical retrace at the same time as
% drawing our fixation point
Screen('DrawDots', window, [xCenter; yCenter], 10, black, [], 2);
vbl = Screen('Flip', window);

% Make a note of the time stamp
iStart = vbl;

% Now we present the isi interval with fixation point minus one frame
% because we presented the fixation point once already when getting a
% time stamp. We use the waitframes functionality to do this without
% the need of a loop

% Draw the fixation point
Screen('DrawDots', window, [xCenter; yCenter], 10, black, [], 2);

% Flip to the screen
vbl = Screen('Flip', window, vbl + ((isiTimeFrames - 1) - 0.5) * ifi);

% Get the presentation time of the ISI
iEnd = vbl;
isiRecDuration = iEnd - iStart;

% Now present the word in continuous loops until the person presses a
% key to respond. We take a time stamp before and after to calculate
% our reaction time. We have used the function getsecs here to achieve
% the same. 
%
% The person should be asked to respond to either the written word or
% the color the word is written in. They make their response with the
% three arrow key. They should press "Left" for "Red", "Down" for
% "Green" and "Right" for "Blue".
%
%
% Additionally, we poll the keyboard each time we update the screen. We
% could alternatively flip to the screen once, then loop over purely
% the keyboard polling and clear the screen once a button is pressed.

while respToBeMade == true

    % Draw the word
    DrawFormattedText(window, char(theWord), 'center', 'center', theColor);

    % Check the keyboard. The person should press the
    [keyIsDown,secs, keyCode] = KbCheck; 
    if keyCode(escapeKey)
        ShowCursor;
        sca;
        return
    elseif keyCode(leftKey)
        response = 1;
        responseTime = GetSecs; % Record time of key press
        respToBeMade = false;
    elseif keyCode(downKey)
        response = 2;
        responseTime = GetSecs; % Record time of key press
        respToBeMade = false;
    elseif keyCode(rightKey)
        response = 3;
        responseTime = GetSecs; % Record time of key press
        respToBeMade = false;
    end

    % Flip to the screen
    vbl = Screen('Flip', window, vbl + (waitframes - 0.5) * ifi);

end

% Record the reaction time, calculated by the use of flip timestamps
rt = vbl - iEnd;

% Record the trial data into out data matrix
respMat_stroop(b).data{1, t} = string(theWord); % record the word which was presented 
respMat_stroop(b).data{2, t} = char(theColor_word); % record the corresponding color it was presented in 
respMat_stroop(b).data{3, t} =  response; % the key the participant responded with 
respMat_stroop(b).data{4, t}  = rt; % reaction time 
respMat_stroop(b).data{5, t}  = isiRecDuration; % presentation time of the ISI
respMat_stroop(b).data{6, t}  = iStart; % time stamp- start time of the trial 
respMat_stroop(b).data{7, t}  = iEnd; % time stamp- End of ISI interval, start of the cue presentation time 
respMat_stroop(b).data{8, t}  = responseTime; % time stamp- time of key press by the participant

end

end

% End of experiment screen. We clear the screen once they have made their % response DrawFormattedText(window, 'Experiment Finished \n\n Press Any Key To Exit',... 'center', 'center', black); Screen('Flip', window); KbStrokeWait; sca;

enbiiiiid commented 2 months ago

%% This is code conducts stroop task in psychtoolbox % KK- March, 2024 % revised- April, 2024

%------------------------------------------------------------------------- % Preliminary Set-up %------------------------------------------------------------------------- % Clear the workspace close all clearvars sca;

%------------------------------------------------------------------------- % Build a GUI to get subject number %------------------------------------------------------------------------- prompt = {'Subject Number'}; % what information do we want from the subject? box = inputdlg(prompt,'Enter Subject Info'); % build the GUI

p.date_time_start = clock; % record time and date of the start of the sesssion !!

if length(box) == 1 % check to make sure something was typed in p.subNum = str2num(box{1}); else return; % if nothing was entered or the subject hit cancel, bail out end

%------------------------------------------------------------------------- % Build an output directory & check to make sure it doesn't already exist %------------------------------------------------------------------------- p.root = pwd; % make an output dir for the subject mkdir(strcat(p.root,'/Subject_Data',filesep,box{1}));

%------------------------------------------------------------------------- % Set-up Psychtoolbox %-------------------------------------------------------------------------

% Setup PTB with some default values PsychDefaultSetup(2);

% Seed the random number generator rng('shuffle')

% Set the screen number to the external secondary monitor if there is one % connected screenNumber = max(Screen('Screens'));

% Define black, white and grey white = WhiteIndex(screenNumber); grey = white / 2; black = BlackIndex(screenNumber);

% Open the screen [window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey, [], 32, 2);

% Flip to clear Screen('Flip', window);

% Query the frame duration ifi = Screen('GetFlipInterval', window);

% Set the text size Screen('TextSize', window, 60);

% Get the centre coordinate of the window [xCenter, yCenter] = RectCenter(windowRect);

% Set the blend funciton for the screen Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');

% Set maximum priority level topPriorityLevel = MaxPriority(window); Priority(topPriorityLevel);

%---------------------------------------------------------------------- % Timing Information %----------------------------------------------------------------------

% Interstimulus interval time in seconds and frames isiTimeSecs = 1; isiTimeFrames = round(isiTimeSecs / ifi);

% Numer of frames to wait before re-drawing waitframes = 1;

%---------------------------------------------------------------------- % Keyboard information %----------------------------------------------------------------------

% Define the keyboard keys that are listened for. We will be using the left % and right arrow keys as response keys for the task and the escape key as % a exit/reset key escapeKey = KbName('ESCAPE'); leftKey = KbName('LeftArrow'); rightKey = KbName('RightArrow'); downKey = KbName('DownArrow');

%---------------------------------------------------------------------- % Colors in words and RGB %----------------------------------------------------------------------

% We are going to use three colors for this demo. Red, Green and blue. colorList = ['red';'green';'blue']; rgbColors = [1 0 0; 0 1 0; 0 0 1];

colortable = table(colorList, rgbColors);

%---------------------------------------------------------------------- % Import the condition matrices %----------------------------------------------------------------------

stroop_condMatrix_pract = readtable ('/Users/neelbazro/Desktop/BBSRC_project/psych_scripts/StroopTrialTypes2.xlsx', 'ReadVariableNames', true); stroop_condMatrix_1 = readtable ('/Users/neelbazro/Desktop/BBSRC_project/psych_scripts/StroopTrialTypesA.xlsx', 'ReadVariableNames', true); stroop_condMatrix_2 = readtable ('/Users/neelbazro/Desktop/BBSRC_project/psych_scripts/StroopTrialTypesB.xlsx', 'ReadVariableNames', true); stroop_condMatrix_3 = readtable ('/Users/neelbazro/Desktop/BBSRC_project/psych_scripts/StroopTrialTypesC.xlsx', 'ReadVariableNames', true); stroop_condMatrix_4 = readtable ('/Users/neelbazro/Desktop/BBSRC_project/psych_scripts/StroopTrialTypesD.xlsx', 'ReadVariableNames', true);

% create a structure from the condition matrices condMatrix(1).data = stroop_condMatrix_1; condMatrix(2).data = stroop_condMatrix_2; condMatrix(3).data = stroop_condMatrix_3; condMatrix(4).data = stroop_condMatrix_4;

%---------------------------------------------------------------------- % Make a master condition and response matrix %----------------------------------------------------------------------

% Define the number of trials % this trial contains four blocks of each condition matrix type numTrials = height(condMatrix(1).data);

% set the number of blocks, in this experiment they were set to 4 numBlocks = 4;

% This is a ... row matrix % the first row will record the word we present, % the second row the color of the word it is written in, % the third row the key participant respond with % fourth row is the time they took to make there response. % The fifth row is the presentation time of the ISI % 6th row is the time stmap- start time of the trial % 7th row is the time stamp- End of ISI interval, start of the cue presentation time % 8th row is the time stamp- time of key press by the participant

for n= 1: numBlocks respMat_stroop(n).data = cell(8, numTrials); end

%---------------------------------------------------------------------- % Experimental loop %----------------------------------------------------------------------

% begin Block Loop for b= 1: numBlocks

% extract conditions table for the given block 
condMatrix_block = condMatrix(b).data;

% begin trial loop for t = 1:numTrials

% The color word and the color it is drawn in
theWord = condMatrix_block{t,1}; 
theColor_word = condMatrix_block{t,2};

% find the corresponding rgb value for the color word 

color_idx = find(colortable{:,1}== string(theColor_word));
theColor = colortable{color_idx,2};

% Cue to determine whether a response has been made
respToBeMade = true;

% If this is the first trial we present a start screen and wait for a
% key-press
if t == 1
    DrawFormattedText(window, 'Name the color \n\n Press Any Key To Begin',...
        'center', 'center', black);
    Screen('Flip', window);
    KbStrokeWait;
end

% Flip again to sync us to the vertical retrace at the same time as
% drawing our fixation point
Screen('DrawDots', window, [xCenter; yCenter], 10, black, [], 2);
vbl = Screen('Flip', window);

% Make a note of the time stamp
iStart = vbl;

% Now we present the isi interval with fixation point minus one frame
% because we presented the fixation point once already when getting a
% time stamp. We use the waitframes functionality to do this without
% the need of a loop

% Draw the fixation point
Screen('DrawDots', window, [xCenter; yCenter], 10, black, [], 2);

% Flip to the screen
vbl = Screen('Flip', window, vbl + ((isiTimeFrames - 1) - 0.5) * ifi);

% Get the presentation time of the ISI
iEnd = vbl;
isiRecDuration = iEnd - iStart;

% Now present the word in continuous loops until the person presses a
% key to respond. We take a time stamp before and after to calculate
% our reaction time. We have used the function getsecs here to achieve
% the same. 
%
% The person should be asked to respond to either the written word or
% the color the word is written in. They make their response with the
% three arrow key. They should press "Left" for "Red", "Down" for
% "Green" and "Right" for "Blue".
%
%
% Additionally, we poll the keyboard each time we update the screen. We
% could alternatively flip to the screen once, then loop over purely
% the keyboard polling and clear the screen once a button is pressed.

while respToBeMade == true

    % Draw the word
    DrawFormattedText(window, char(theWord), 'center', 'center', theColor);

    % Check the keyboard. The person should press the
    [keyIsDown,secs, keyCode] = KbCheck; 
    if keyCode(escapeKey)
        ShowCursor;
        sca;
        return
    elseif keyCode(leftKey)
        response = 1;
        responseTime = GetSecs; % Record time of key press
        respToBeMade = false;
    elseif keyCode(downKey)
        response = 2;
        responseTime = GetSecs; % Record time of key press
        respToBeMade = false;
    elseif keyCode(rightKey)
        response = 3;
        responseTime = GetSecs; % Record time of key press
        respToBeMade = false;
    end

    % Flip to the screen
    vbl = Screen('Flip', window, vbl + (waitframes - 0.5) * ifi);

end

% Record the reaction time, calculated by the use of flip timestamps
rt = vbl - iEnd;

% Record the trial data into out data matrix
respMat_stroop(b).data{1, t} = string(theWord); % record the word which was presented 
respMat_stroop(b).data{2, t} = char(theColor_word); % record the corresponding color it was presented in 
respMat_stroop(b).data{3, t} =  response; % the key the participant responded with 
respMat_stroop(b).data{4, t}  = rt; % reaction time 
respMat_stroop(b).data{5, t}  = isiRecDuration; % presentation time of the ISI
respMat_stroop(b).data{6, t}  = iStart; % time stamp- start time of the trial 
respMat_stroop(b).data{7, t}  = iEnd; % time stamp- End of ISI interval, start of the cue presentation time 
respMat_stroop(b).data{8, t}  = responseTime; % time stamp- time of key press by the participant

end

end

% End of experiment screen. We clear the screen once they have made their % response DrawFormattedText(window, 'Experiment Finished \n\n Press Any Key To Exit',... 'center', 'center', black); Screen('Flip', window); KbStrokeWait; sca;