0

I'm using psychtoolbox in MATLAB and I want to get a participant to rate the distortion of a string of images from 0-9. I've tried using GetChar, but when I run the script it doesn't wait for the user to give a response but just moves onto the next screen. Any advice on how I can fix this?

%using a loop to show images
for k=1:290
texture1(k)=Screen('MakeTexture',w,images{k});    
end
for k=1:145
Screen('DrawTexture',w, texture1(k), [], leftposition);
Screen('DrawTexture',w, texture1(k+145), [], rightposition);
Screen('DrawLines', w, allCoords,...
lineWidthPix, black, [xCenter yCenter], 2);
Screen(w,'Flip');
pause(0.2);
end


%rating text
DrawFormattedText(w,'Rate distortion 0-9','center','center',[255 255  255]);
Screen(w,'Flip');
GetChar();


%press space to finish
DrawFormattedText(w,'press space to finish','center','center',[255 255 255]);
Screen(w,'Flip');


% Wait for a key press
KbStrokeWait;

% Clear the screen
sca;
Emily
  • 29
  • 1
  • 7

1 Answers1

0

There's a few things going on here: You're only looking for key presses after your loop is finished, and you're not storing the result of the key press.

GetChar is also messy (you would probably want to call FlushEvents to clear the queue, or you might run into (from help GetChar):

If a character was typed before calling GetChar then GetChar will return that character immediately.

One alternative is demonstrated in the example below. It uses KbWait, which provides very similar functionality, with a little more work (i.e. converting the key code to a char). Additionally, it implements a 5ms delay between key checks, which helps prevent accidental key bouncing (counting single presses as multiple presses).

This example opens a small window in the upper left corner, displays the current loop iteration in the center of the screen, and waits for a single press to continue. It also records the times of the key presses in times.

Screen('Preference', 'SkipSyncTests', 2);
[win, rect] = Screen('OpenWindow', 0, [0 0 0], [15 15 400 400]);
Screen('TextSize', win, 20);

answers = cell(1, 5);
times = zeros(1, 5);
ref_time = GetSecs;

for ii = 1:5
    DrawFormattedText(win, num2str(ii), 'center', 'center', [255 255 255]);
    Screen(win, 'Flip');
    [times(ii), key_code] = KbWait;
    answers{ii} = KbName(find(key_code));
    WaitSecs(0.1); %# Wait an extra 100ms for better debouncing
end

times = times - ref_time;

sca;
alexforrence
  • 2,724
  • 2
  • 27
  • 30