Hello,
I've managed to solve this with the help from the Mathworks Technical Support Team. It seems to be a problem with Java.
I first tried to set ulimit -n and ulimit -u to the ones defined here https://mathworks.com/help/parallel-computing/recommended-system-limits-for-macintosh-and-linux.html but it didn't work.
EDIT:
I found that my error was related to the Java version shipped with MATLAB. Two options worked for me.
Option 1:
Install Amazon Corretto Java https://www.mathworks.com/support/requirements/openjdk.html and set this as the default JRE (https://es.mathworks.com/matlabcentral/answers/103056-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-on-macos)
This option doesn't require root access, but in my case, MATLAB took over a minute to start.
Option 2:
Increasing the hard open files limit. I'm not sure why this worked but ulimit -n didn't.
For me this solved the problem, no need to change to Corretto Java:
echo > /etc/security/limits.d/systemd.conf "* hard nofile 65536"
I needed to reboot after setting this file so the changes were loaded.
I want to thank the Mathworks Technical Support Team for their help and patience.