MATLAB + Mac Keyboard Recognition

MacOS Sonoma 14.0 (23A344) + MATLAB R2023b Update 1 (23.2.0.2380103) 64-bit (maci64)

My PI and I are designing an experiment on a new desktop Mac that takes over the whole screen. When it gets time for the participant to enter some information or press SPACE or ESC, MATLAB stops responding. I’m pretty sure it doesn’t recognize the keyboard, a common issue when running PTB scripts. We have to force quit whenever it gets stuck, so I don’t have an output to share.

We have restarted and updated MATLAB and the whole desktop. Does anyone have ideas for troubleshooting?

Thanks!

Yes, this is due to persistent macOS bugs with privacy and security settings. You MUST explicitly add MATLAB to Privacy & Security - I add it to Accessibility, Input Monitoring and Full Disk Access just to be sure. Every update or with any problems you must remove and readd it to these positions. Recently even that is not enough and so I must always start MATLAB via my Terminal – that must ALSO be added to Privacy and Security. To start MATLAB from terminal use matlab &. To test this has worked before your experiment you can use this snippet:

WaitSecs(0.5); [a,b,c] = KbWait; KbName(b)

This should wait until you press a key then echo that keyname back, if it does then your keyboard is working and your experiment should work.

2 Likes

Just a tiny and almost irrelevant note, but. Just running KbName without any input arguments waits one second and then calls KbCheck in a tight loop until a keypress is detected, reporting the key pressed. so you only have to invoke that.

2 Likes

What Ian and Dee said.

As a general comment, macOS 14 Sonoma is not yet officially supported, and won’t be for a while. I couldn’t even run or test it if I wanted, as Apple decided that my 2017 Intel MBP is not worthy of operating system upgrades anymore. But also, given the poor quality of macOS upgrades and the miserable quality control at Apple, the sane thing to do in any case is to wait for the better part of a year before upgrading to a new major macOS release.

And Apple Silicon Macs right now are almost certainly unfit for any data collection with any software right now, if visual stimulation with precise timing or other low level control is required, so I hope for you that your “new desktop Mac” is only used for designing the experiment, not for running it, if timing matters.

1 Like

Thank you so much for your thoughts. As far as timing goes, this is what we’re hoping the experiment would look like: We would enter the participant number and hit ENTER. The participant would hear an audio sample and see a set of images. The computer will then begin recording the participant’s audio response to the images. The participant will hit a button to stop the recording when finished. This will be a loop of about 20 trials. Overall, I don’t think that timing precision would be a huge issue, but we would love to have the keyboard communicate.

Thank you so much for your tips. I added MATLAB and Terminal to Accessibility, Input Monitoring, and Full Disk Access. I restarted the whole system.

However, when I opened Terminal, I was unable to interact with it by typing; it did not respond to any keystroke unless it was a keyboard shortcut. I wonder if I don’t have administrator privileges, if the keyboard just doesn’t like to communicate with certain systems, or I just don’t know how to use Terminal.

I instead used CMD + Shift + N and entered matlab & into the New Command window. I clicked Run and the output was this:
[Command not found: matlab]
[Could not create a new process and open a pseudo-tty.]

When I ticked the box and clicked Run, this was the output:
[Command not found: /bin/false]
[Could not create a new process and open a pseudo-tty.]

I also repeated these methods using the file name “MATLAB_R2023b” with the ampersand, box unticked. The outputs were similar:

[Command not found: MATLAB_R2023b]
[Could not create a new process and open a pseudo-tty.]
and
[Command not found: /bin/false]
[Could not create a new process and open a pseudo-tty.]

I tested whether MATLAB recognizes keystrokes using the snippet you shared:

It works after 1 second, just as @dcnieho suggested might happen. Does that mean that MATLAB does recognize my keyboard? Any additional thoughts from anyone? Thanks in advance.

I didn’t know that, very cool, thanks Dee!!!

There is something seriously wrong with your system if you can’t type text in Terminal. Check if you have some other software installed that takes over the keyboard (perhaps your school is using some sort of restricted computing management system, or you have malware or something). Try to create a new user account and check there.

You mean you get the name back of the key you typed? If so then it is working, no need for Terminal, though the fact Terminal doesn’t work should still be resolved…

Regarding timing, the point is you want to run your experiment in the most reliable way possible. I totally love my Macbook Air M2 overall (16hr battery life, faster and smoother than my Dell workstation for many tasks), but I would never use it for PTB data collection (experimental data is priceless, I do not want to risk artifacts). You could use a DAQ to record a photodiode, microphone and PTB trigger signals so that you have an independent time confirmation, then you have a groundtruth…

1 Like

What Ian says, with the comment that if errors in your visual presentation timing, response collection etc. on the order of 50 - 100 milliseconds don’t matter, then probably even the Apple Silicon machine would do. With the disclaimer that I have never seen a Apple Silicon machine in the flesh, so those are just my untested working assumptions about the current state of things, based on 2nd hand reports on the forum.

-mario

An update (Feb 29, 2024): MacOS Sonoma 14.1.1 (23B81) + MATLAB R2023b Update 1 (23.2.0.2380103) 64-bit (maci64)

I was able to debug by uncommenting the PsychDebugWindowConfiguration. When this code is active, the full screen becomes transparent so I can interact with the MATLAB Command Window. I use the sca function to close out of the full screen mode when needed.

However, when I comment the PsychDebugWindowConfiguration line back out, I can no longer interact with the Command Window. The biggest issue is at the beginning, when I have to enter the Experimenter and Participant IDs, but the program will not register my keyboard entries. I have taken peeks at the getStringInputWithQuestions and initializeParticipant scripts, but I’m still not familiar with how those work. They come with the +cog_comm_tools package from PsychToolbox.

Anywho, the main goal now is for me to be able to pilot this experiment by being able to type in the Experimenter and Participant IDs, either by accessing the Command Window with an opaque screen over the MATLAB window or without needing to click into the Command Window before interacting with the keyboard. Any help would be appreciated.

That is not part of PTB btw.

Again if you can’t enter text into Terminal, something is very wrong on your machine. For me to get the keyboard to work (macOS Sonoma 14.3.1 MATLAB 2023b update 7 latests PTB), I start matlab from the terminal:

arch -x86_64 matlab &

THen I can use the keyboard without issue. Did you try this?

If i understand you correctly, you cannot type into the command window because the PTB screen is blocking it? That would make sense. Ask for this input, these IDs, before opening the PTB screen

This sounds like a case for help PsychPaidSupportAndServices paid support.

Great idea! I tried this and it said that it didn’t recognize the participantID line. Is there a way to ask for these IDs in a smaller screen that doesn’t quite go full-screen? Here’s the line:

participantID = initializeParticipant(window); %this is an elaborate piece of code with scripts nested within nested scripts

One solution is to just change this to:

participantID = '1111'; %change the 1111 for each participant

but I’m afraid that my lab assistant will accidentally change the script or we’ll overwrite the participant data folders. The initializeParticipant script asks before overwriting participant data.

Another solution would be to manipulate the new window, either to decrease its size so I can access the MATLAB command window, or to make it transparent only when executing the initializeParticipant command, then going back to the opaque screen.

The final solution would be to generate random 4-digit strings and then give a warning before overwriting a redundant participant ID. I’m not sure how to do either of these. What are your thoughts?

Thanks for your help. At this point, I’m trying to find a workaround in the script, by either randomizing a new participant ID with each participant or manipulating the new window that pops up.

Here’s the offending line:

participantID = initializeParticipant(window);

Here’s solution 1a:

participantID = '1111'; %manually change 1111 to a new participant ID before running the experiment

Here’s solution 1b:

function participantID = generateUniqueID()
  % Initialize an empty string for the ID
  participantID = '';

  % Loop until the ID has 4 unique digits
  while length(participantID) < 4
    % Generate a random digit between 0 and 9
    digit = randi(10) - 1;

    % Check if the digit is already in the ID
    if ~any(participantID == digit)
      % If not, append the digit to the ID
      participantID = [participantID num2str(digit)];
    end
  end
end

% Generate a unique participant ID
participantID = generateUniqueID();

Here’s solution 1c:

% Generate a random vector of 4 digits (may contain duplicates)
digits = randi(10, 1, 4) - 1;

% Sort the digits in ascending order
uniqueDigits = unique(digits);

% If less than 4 unique digits were generated, repeat
while length(uniqueDigits) < 4
  % Generate additional random digits until we have 4 unique ones
  additionalDigits = randi(10, 1, 4 - length(uniqueDigits)) - 1;
  uniqueDigits = [uniqueDigits, additionalDigits];
  uniqueDigits = unique(uniqueDigits);
end

% Convert the unique digits to a string and assign to participantID
participantID = num2str(uniqueDigits);

Here’s solution 2a:

%make the participantID window temporarily smaller so I can access the Command Window
Screen ('CloseAll');

And solution 2b:

%make the participantID window temporarily transparent so I can access the Command Window
Screen ('CloseAll');

What are your thoughts? I’m not that familiar yet with MATLAB so I’m not sure how to flesh out solution 2a or 2b, or if my syntax is even correct for any of solutions 1a-c, but any ideas would help.

Thank you!

Thanks for the idea. I might reach out to them soon. I do have a solution, but I’d have to accept the risk that my script will be accidentally modified.