Cognitive Neurophysiology Laboratory

CNL Wiki

Docs: MATLAB mex issue on mac

Updated on September 22, 2023

The error #

When trying to run Neuralynx data extraction (eg through PDM) may encounter error such as this:

“`

Processing CSC80.mat
Processing CSC80.mat failed.
MException with properties:

identifier: ‘MATLAB:mex:ErrInvalidMEXFile’
message: ‘Invalid MEX-file ‘~/Documents/CODE/Nlx2Mat_relDec11/binaries/Nlx2MatCSC_v3.mexmaci64’: dlopen(~/Documents/CODE/Nlx2Mat_relDec11/binaries/Nlx2MatCSC_v3.mexmaci64, 0x0006): Library not loaded: @loader_path/libmex.dylib↵ Referenced from: <0E7EF85C-C3D0-35CE-6AD2-4E1B1DD4D804> ~/Documents/CODE/Nlx2Mat_relDec11/binaries/Nlx2MatCSC_v3.mexmaci64↵ Reason: tried: ‘~/Documents/CODE/Nlx2Mat_relDec11/binaries/libmex.dylib’ (no such file), ‘/usr/local/lib/libmex.dylib’ (no such file), ‘/usr/lib/libmex.dylib’ (no such file, not in dyld cache)’
cause: {}
stack: [9×1 struct]
Correction: []

“`

Also see:

  • https://www.mathworks.com/matlabcentral/answers/763346-mexfile-error-libmex-dylib-not-loaded
  • https://stackoverflow.com/questions/48458660/library-not-loaded-loader-path-libmex-dylib-in-matlab

The fix #

I tried all of the below steps based on answers to the above links. Therefore I’m not totally sure which combination of things is actually necessary, other than using the latest nlx2mat mex files which was the last step I tried before it started working.

 

Disable SIP #

Checking if SIP enabled in terminal. Navigate to Applications/Utilities, run csrutil status eg:
“`
(base) MAC:~ usr$ cd /Applications/Utilities/
(base) MAC:Utilities usr$ csrutil status
System Integrity Protection status: enabled.
“`
Disable SIP (on mac studio)
  • start mac in recovery mode
    •  shutdown
    • press and hold power key (+5 s)
    • options
    • select user, input pwd
  • Utilities tab (top left) > Terminal
  • Run the command `crsutil disable`

Will need to re-allow developer for macfuse to run to mount Hoffman.

(2023-09-22: since disabling SIP is not recommended plan to try re-enabling to see if mex files still work)

 

mex -setup #

In MATLAB:

“`
mex -setup
“`

For me this output:

“`
MEX configured to use ‘Xcode with Clang’ for C language compilation.

To choose a different language, select one from the following:
mex -setup C++
mex -setup FORTRAN
“`

 

Using latest nlx2mat mex files #

As of 2023-09-22 the latest nlx2mat files were released Dec 2015 and can be found here: https://www.urut.ch/new/serendipity/index.php?/pages/nlxtomatlab.html

Neuralynx do not develop or support the mac/linux versions of nlx2mat.

(2023-09-22: plan to add these to the CNL github also)