MATLAB complains about a missing or invalid MEX file, what should I do?
The fieldtrip version on the ftp server includes compile mex files for the most common platforms. Hoever, it might be that you have a platform (OS and MATLAB combination) on which the precompiled mex files are not included, and then you'll have to compile them yourself.
The source code for the mex files is included with the fieldtrip release, at the same place as where the mex files are supposed to be located. Since most of the mex files are low-level functions, you will mainly find them in the “private” directories. You can recompile the mex files for your platform using the following commands:
cd ../somehere/private mex meg_leadfield1.c mex plgndr.c mex read_24bit.c
The following files all depend on some shared code in geometry.c and geometry.h, hence the compilation process is slighty different for these:
mex -I. -c geometry.c mex -I. -c solid_angle.c ; mex -o solid_angle solid_angle.o geometry.o mex -I. -c lmoutr.c ; mex -o lmoutr lmoutr.o geometry.o mex -I. -c ptriproj.c ; mex -o ptriproj ptriproj.o geometry.o mex -I. -c ltrisect.c ; mex -o ltrisect ltrisect.o geometry.o mex -I. -c routlm.c ; mex -o routlm routlm.o geometry.o mex -I. -c plinproj.c ; mex -o plinproj plinproj.o geometry.o
If you are on a Windows computer, the compiled object files with have extension *.obj instead of *.o and hence you then should do
mex -I. -c geometry.c mex -I. -c solid_angle.c ; mex solid_angle.c solid_angle.obj geometry.obj mex -I. -c lmoutr.c ; mex lmoutr.c lmoutr.obj geometry.obj mex -I. -c ptriproj.c ; mex ptriproj.c ptriproj.obj geometry.obj mex -I. -c ltrisect.c ; mex ltrisect.c ltrisect.obj geometry.obj mex -I. -c routlm.c ; mex routlm.c routlm.obj geometry.obj mex -I. -c plinproj.c ; mex plinproj.c plinproj.obj geometry.obj
After compilation the mex files are automatically at the right location.
Note that most of the mex files include a m-file wrapper which will try to do an auto compilation. Again, the autocompilation has not (and can not) be tested on all possible platforms, so your mileage may vary.
If you are using the Visual Studio Compiler and get an error similar to:
error LNK2005: mexFunction already defined in ptriproj.obj
then try this line replacing ptriproj with the file you are trying to compile.
mex -I. -c ptriproj.c ; mex ptriproj.c geometry.obj
Note in Linux: If you are get the error message “cannot find -lstdc++”, you can install the libstdc++5 package using your package manager. If, after the install of libtdc++5, the lstdc++ error persists, just do a symbolic link like
ln -s /usr/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so
and it should work.