Method of adjustment response task

Hi,
I am using MATLAB R2020a on windows 10. This is my first time using Psychtoolbox and I am trying to programme a method of adjustment response task where one randomly presented face image from a morphed continuum appears onscreen and the left and right arrow keys are used to adjust this face by flipping through the continuum. Basically, this just means that when the right key is pressed the next image in the file should appear and when the left key is pressed the previous image should appear. The participants will choose one image from the continuum and submit as their response. Code and error message are listed below. Any help will be much appreciated!

This is the error message I am recieving when pressing right/left key: Index in position 1 exceeds array bounds (must not exceed 1).

Error in Experiment_2 (line 128)
if (keyCode(right, ‘Value’)== 1)

This is the code I have tried using for the response task based on the KbDemo part 4:

stimulus = length(firstface); % number of faces to sample from image file firstface
af = randi(stimulus,Ntrials,Blocks); % random start face in adjustment task
af1 = af(i,j); % random face number
% Show adjustment face
texture = Screen(‘MakeTexture’, window, firstface{af1});
Screen(‘DrawTexture’, window, texture);
Screen(‘Flip’, window);
FlushEvents(‘keyDown’); % remove queue of events for key presses
t1 = GetSecs;
time = 0;
while time < inf % max wait time
[keyIsDown, t2, keyCode] = KbCheck; % determine state of keyboard
time = t2-t1;
if keyIsDown % has a key been pressed?
if (keyCode(right, ‘Value’)== 1)
if af1 == stimulus
af1 = 1;
else
af1 = af1+1;
end
texture = Screen(‘MakeTexture’, window, firstface{af1});
Screen(‘DrawTexture’, window, texture);
Screen(‘Flip’, window);
end
if (keyCode(left, ‘Value’)== 1)
if af1 == stimulus
af1 = stimulus;
else
af1 = af1-1;
end
texture = Screen(‘MakeTexture’, window, firstface{af1});
Screen(‘DrawTexture’, window, texture);
Screen(‘Flip’, window);
end
if (keyCode(done, ‘Value’) ==1)
response_time(i,j) = time;
response(i,j) = af1;
end
break;

Hi,

Here is the description of keyCode :

keyCode A 256-element logical array. Each bit
within the logical array represents one keyboard key.
If a key is pressed, its bit is set, othewise the bit
is clear. To convert a keyCode to a vector of key
numbers use FIND(keyCode). To find a key’s keyNumber
use KbName or KbDemo.

Transform if (keyCode(right, ‘Value’)== 1) into if (keyCode(right)== 1)
Also, it can be simplified in if keyCode(right)

Best,
Ben

Thank you, Ben! Your response was very helpful.
/Anette