Sunday, July 08, 2007


I had learnt somethings from my experience building mex files for C-MATLAB interaction. Below I list some helpful points to keep in mind in cases of frustration:
  • "mexing" in Linux/Unix requires a new setup of compilers.

  • Make sure you have compatible gcc with the MATLAB version installed.

  • Sometimes commenting lines of code using '//' might produce errors. Using '/*....*/' is safe.

  • mexing on 32-bit Linux produces .mexglx executables and those are not executable on 64-bit Linux machines which need .mexa64 executables.

  • mexPrintf statements can make your program really slow.

  • Having too many "mx.." statements in the C-code might make the code run slower possibly because you are just repeating what MATLAB might already be doing without your C-code. Remember MATLAB does use some of its built-in C-code for speeding up certain operations. As an example if you have to handle structures in your C-code use "mx.." statements to pass parameters but use the "struct" variables in the C-code not mxArrays.

  • Initially try to balance of how much of your code you want in C-style and how much in MATLAB. After you get familiarized with "mex" code try to code most of it in C and then use MATLAB as an "interace" for experimenting with your code.

  • If building huge systems which you will probably do only after getting familiarized with "mex" forget MATLAB and use development tools like Visual Studio etc. for building and debugging then just add the interface. Debuggin on smaller projects can be handled effectively using mexPrintf which later have to be minimal.

No comments: