0

I'm running into trouble collecting keyboard inputs for two psychological tasks, a pavlovian and instrumental learning script. I'll show the instrumental script in hopes that solving the problem in the instrumental script will transfer to the Pavlovian script. I'm running on a 2018 MacBook Pro with Touch Bar, High Sierra.

What I want is for the window to close if I press escape, and during the trial loop, count the number of left, up, and right arrow keypresses so the outcome image is shown when the key is pressed nine times. But the blue window just stays on the screen and is unresponsive to keyboard input. Actually, at the moment, the script just stays stuck on the intro window with the introduction text.

Instrumental.m

Here's what I've tried:

Using KbCheck instead of KbWait

Erasing the While loop

Putting the keyboard input code at different places in the code

Using different KbNames

Using a different index for KbWait

Thanks so much for your help!

Lauren
  • 1
  • 1

1 Answers1

0

There are several pieces missing to be able to run your script, for example PITGUI(), and the image files. But from looking at the code, a few things to fix:

strings should use single rather than double quotes:

change:

fname = sprintf('%s %s', InputDataStruct.filename, ".txt");

to:

fname = sprintf('%s %s', InputDataStruct.filename, '.txt');

Right now, keyIsDown will never evaluate to true, likely because KbWait was swapped in for KbCheck:

change:

[secs, keyCode] = KbWait(1);

to:

[keyIsDown, secs, keyCode] = KbCheck;

to display the errors:

change:

catch
    Screen('CloseAll')
    rethrow(lasterror)   
end

to:

catch e
    Screen('CloseAll')
    rethrow(e)   
end
DMR
  • 1,479
  • 1
  • 8
  • 11
  • So I've made those changes but there are still at least two problems. It throws an error at this line: fname = sprintf('%s %s', InputDataStruct.filename, '.txt'); furthermore, if I comment this chunk out, it still has trouble counting the number of keypresses like I want to. Although I can press 'w' to quit. It seems like the variable O1count, O2count, etc.. are not even being created? Any ideas? https://www.dropbox.com/s/d1e9e67j2rus4yc/Instrumental.m?dl=0 is the new version. – Lauren Oct 20 '18 at 00:01
  • It's hard to debug precisely without having access to your inputs. But on the filename error, you have a space in your 'sprintf' call, so your assembled file name ends up being 'name .txt' with a space. Instead, you can just use fname = sprintf('%s.txt', InputDataStruct.filename) . – DMR Oct 20 '18 at 16:00
  • On the counts, the count variables are being incremented, which you can see if you add this command after the 'while' loop to print the current values to the MATLAB console: disp('Counts:'); disp(O1count); disp(O2count); disp(O3count); The issue is in your logic is setup regarding the trial loop. Each arrow press is also registering as multiple presses, because the button loop doens't wait for the keypress to be released. – DMR Oct 20 '18 at 16:24
  • Okay, so how would I wait for the keypress to be released? Just add KbReleaseWait right before the loop ends? Thank you so much! – Lauren Oct 22 '18 at 15:05
  • That would work, in following the while ~keyIsDown loop. You would also want to reset the keyCode variable on each trial, similar to how keyIsDown is reset to 0. – DMR Oct 29 '18 at 01:15