I am trying to analyse responses from specific conditions to calculate their means later. My experiment presents participants with asian or black faces and asks for a keyboard response. However, when I try to select only the trials which had correct responses, the variable "asian_resp" does not get created, while "black_resp" does. Could someone help me find the problem? My code is attached.(last 10 lines of code have the important part)
Thanks!
%creating random array of ones and twos to use to define conditions later-
%to counterbalance
n = 40;
v = [ones(n, 1); 2*ones(n, 1)];
vshuffle = v(randperm(2*n));
Screen('Preference', 'SkipSyncTests', 1);
screen_id = max(Screen('Screens'));
[w,rect] = Screen('OpenWindow', screen_id, 127, []);
Screen('DrawText', w , 'Press A if you see an Asian face and B if you see a Black face' , (rect(3)/2-700), rect(4)/2);
Screen('Flip', w);
WaitSecs(2);
%creating random array of 1 to 20 numbers to use to get random pictures
a=randperm(20);
b=randperm(20);
c=randperm(20);
d=randperm(20);
e=[a,b,c,d];
for k=1:80
Screen('DrawText', w , '+' ,rect(3)/2, rect(4)/2, 255);
Screen('Flip', w);
WaitSecs(0.5);
%if vshuffle is 1 then display asian images- to counterbalance
if vshuffle(k)==1
cur_im=imresize(Asian_images{1,e(k)},0.6);
im_tex = Screen('MakeTexture', w, cur_im);
Screen('DrawTexture', w, im_tex);
start_time=Screen('Flip', w);
KbWait;
%if vshuffle is 2 display black images- to counterbalance
elseif vshuffle(k)==2
cur_im=imresize(Black_images{1,e(k)},0.6);
im_tex = Screen('MakeTexture', w, cur_im);
Screen('DrawTexture', w, im_tex);
start_time=Screen('Flip', w);
KbWait;
exp_codes = [KbName('a') , KbName('b')];
show_stim = 1;
while show_stim == 1
[keyIsDown, secs, keyCode] = KbCheck;
pressed_code = find(keyCode == 1);
if keyIsDown == 1 && ismember(pressed_code, exp_codes) == 1
show_stim = 0;
end
end
pp_response(k)=pressed_code;
resp_data(k)=secs-start_time;
% if response is "a" and vshuffle is 1 then the correct answer is given,
% only store correct answer response time
if pp_response(k)==65 && vshuffle(k)==1
asian_resp(k)=resp_data(k);
%omit any 0 values because they would be for black trials
asian_resp(asian_resp==0)=[]
% if response is "b" and vshuffle is 2 then the correct answer is given,
% only store correct answer response time
elseif pp_response(k)==66 && vshuffle(k)==2
black_resp(k)=resp_data(k)
%omit any 0 values because they would be for asian trials
black_resp(black_resp==0)=[]
end
end
Screen('Close', im_tex)
end
sca;

 Accepted Answer

Here is a copy of the code you posted, but with indentation:
%creating random array of ones and twos to use to define conditions later-
%to counterbalance
n = 40;
v = [ones(n, 1); 2*ones(n, 1)];
vshuffle = v(randperm(2*n));
Screen('Preference', 'SkipSyncTests', 1);
screen_id = max(Screen('Screens'));
[w,rect] = Screen('OpenWindow', screen_id, 127, []);
Screen('DrawText', w , 'Press A if you see an Asian face and B if you see a Black face' , (rect(3)/2-700), rect(4)/2);
Screen('Flip', w);
WaitSecs(2);
%creating random array of 1 to 20 numbers to use to get random pictures
a=randperm(20);
b=randperm(20);
c=randperm(20);
d=randperm(20);
e=[a,b,c,d];
for k=1:80
Screen('DrawText', w , '+' ,rect(3)/2, rect(4)/2, 255);
Screen('Flip', w);
WaitSecs(0.5);
%if vshuffle is 1 then display asian images- to counterbalance
if vshuffle(k)==1
cur_im=imresize(Asian_images{1,e(k)},0.6);
im_tex = Screen('MakeTexture', w, cur_im);
Screen('DrawTexture', w, im_tex);
start_time=Screen('Flip', w);
KbWait;
%if vshuffle is 2 display black images- to counterbalance
elseif vshuffle(k)==2
cur_im=imresize(Black_images{1,e(k)},0.6);
im_tex = Screen('MakeTexture', w, cur_im);
Screen('DrawTexture', w, im_tex);
start_time=Screen('Flip', w);
KbWait;
exp_codes = [KbName('a') , KbName('b')];
show_stim = 1;
while show_stim == 1
[keyIsDown, secs, keyCode] = KbCheck;
pressed_code = find(keyCode == 1);
if keyIsDown == 1 && ismember(pressed_code, exp_codes) == 1
show_stim = 0;
end
end
pp_response(k)=pressed_code;
resp_data(k)=secs-start_time;
% if response is "a" and vshuffle is 1 then the correct answer is given,
% only store correct answer response time
if pp_response(k)==65 && vshuffle(k)==1
asian_resp(k)=resp_data(k);
%omit any 0 values because they would be for black trials
asian_resp(asian_resp==0)=[]
% if response is "b" and vshuffle is 2 then the correct answer is given,
% only store correct answer response time
elseif pp_response(k)==66 && vshuffle(k)==2
black_resp(k)=resp_data(k)
%omit any 0 values because they would be for asian trials
black_resp(black_resp==0)=[]
end
end
Screen('Close', im_tex)
end
sca;
As you can see, the parts of the code for capturing the keyboard input from the user and checking whether the user's response is correct exist inside the elseif vshuffle(k)==2 block, which means that the condition pp_response(k)==65 && vshuffle(k)==1 is always false (since vshuffle(k) is 2), so the line that assigns a value to asian_resp(k) will never be executed.
The indentation also helps make it pretty clear how to fix this: move the input-gathering and checking blocks to after the vshuffle(k)==2 block (essentially just moving the second-to-last end upward to close the vshuffle if block). Like this:
%creating random array of ones and twos to use to define conditions later-
%to counterbalance
n = 40;
v = [ones(n, 1); 2*ones(n, 1)];
vshuffle = v(randperm(2*n));
Screen('Preference', 'SkipSyncTests', 1);
screen_id = max(Screen('Screens'));
[w,rect] = Screen('OpenWindow', screen_id, 127, []);
Screen('DrawText', w , 'Press A if you see an Asian face and B if you see a Black face' , (rect(3)/2-700), rect(4)/2);
Screen('Flip', w);
WaitSecs(2);
%creating random array of 1 to 20 numbers to use to get random pictures
a=randperm(20);
b=randperm(20);
c=randperm(20);
d=randperm(20);
e=[a,b,c,d];
for k=1:80
Screen('DrawText', w , '+' ,rect(3)/2, rect(4)/2, 255);
Screen('Flip', w);
WaitSecs(0.5);
%if vshuffle is 1 then display asian images- to counterbalance
if vshuffle(k)==1
cur_im=imresize(Asian_images{1,e(k)},0.6);
im_tex = Screen('MakeTexture', w, cur_im);
Screen('DrawTexture', w, im_tex);
start_time=Screen('Flip', w);
KbWait;
%if vshuffle is 2 display black images- to counterbalance
elseif vshuffle(k)==2
cur_im=imresize(Black_images{1,e(k)},0.6);
im_tex = Screen('MakeTexture', w, cur_im);
Screen('DrawTexture', w, im_tex);
start_time=Screen('Flip', w);
KbWait;
end
exp_codes = [KbName('a') , KbName('b')];
show_stim = 1;
while show_stim == 1
[keyIsDown, secs, keyCode] = KbCheck;
pressed_code = find(keyCode == 1);
if keyIsDown == 1 && ismember(pressed_code, exp_codes) == 1
show_stim = 0;
end
end
pp_response(k)=pressed_code;
resp_data(k)=secs-start_time;
% if response is "a" and vshuffle is 1 then the correct answer is given,
% only store correct answer response time
if pp_response(k)==65 && vshuffle(k)==1
asian_resp(k)=resp_data(k);
%omit any 0 values because they would be for black trials
asian_resp(asian_resp==0)=[]
% if response is "b" and vshuffle is 2 then the correct answer is given,
% only store correct answer response time
elseif pp_response(k)==66 && vshuffle(k)==2
black_resp(k)=resp_data(k)
%omit any 0 values because they would be for asian trials
black_resp(black_resp==0)=[]
end
Screen('Close', im_tex)
end
sca;
To apply proper indentation to your code in the MATLAB Editor, highlight the code and hit Ctrl+I or right-click and select "Smart Indent".

More Answers (0)

Products

Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!