Keycode response and response times displaying 0

I am trying to run a simple experiment to see if asians respond faster to asian faces as compared to black. I have 80 trials, however, when I collect the keypress responses by using keycode and response times, some of the values are 0 instead of the input. Please see the image attached. Could someone help in finding the error? My code is attached.
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

 采纳的回答

I believe this problem will be prevented by doing what I described in my answer to another question about this code.
To explain the cause: Since the while loop and subsequent if-elseif block are within the vshuffle(k) == 2 block, the variables pp_response and resp_data do not get assigned values for values of k where vshuffle(k) == 1. Since the for loop is appending values to the end of those vectors as it goes through values of k, MATLAB fills in the missing elements with zeros, which is what you see. So all those zero elements correspond to places where vshuffle is 1.
Restructure the code in the way described in the other answer, and these zeros will not be there.

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Timing and presenting 2D and 3D stimuli 的更多信息

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by