Department of Engineering

IT Services

Matlab: configuring mex

mex is a program used when compiling C, C++ or Fortran code to work with Matlab. You can run it from within matlab or from the compile line. Depending on the way you run it and the language you use, you may need to configure it for use on our central system.

Using mex with C from inside Matlab

Try the following from inside matlab - it copies a demo file into a temporary folder, compiles it then runs it.

 cd /tmp
 demofile = [matlabroot '/extern/examples/eng_mat/engdemo.c'];
 copyfile(demofile, '.');
 optsfile = [matlabroot '/bin/engopts.sh'];
 mex('-f', optsfile, 'engdemo.c');
 !./engdemo

You might get a warning

Warning: You are using gcc version "4.3.1".  The earliest gcc version supported
         with mex is "4.1".  The latest version tested for use with mex is "4.2".
         To download a different version of gcc, visit http://gcc.gnu.org 

but the final line should work. On our system you should see a matlab window appearing when you run engdemo.

If it doesn't work, trying running mex from the linux command line as described in the next section.

Using mex with C from outside Matlab

If then you open a terminal window and do the following from the unix command line

   cd /tmp
   mex -f /usr/local/apps/matlab/matlab2009b/bin/engopts.sh engdemo.c

you should also be able to produce an engdemo program but when you try to run engdemo you're likely to get an error message like the following

./engdemo: error while loading shared libraries: libeng.so: cannot open shared object file: No such file or directory

You need to type the following first

export LD_LIBRARY_PATH=/usr/local/apps/matlab/matlab2009b/bin/glnxa64:/usr/local/apps/matlab/matlab2009b/sys/os/glnxa64:$LD_LIBRARY_PATH

The /usr/local/apps/matlab/matlab2009b/ part of this command might change in the future. To find its current value, type matlabroot inside matlab.

Using mex with fortran from inside Matlab

mex is set up to use g95, a fortran compiler we don't have, so you'll need to create your own configuration file. The following unix command copies the system default configuration file into your home directory.

  cp /usr/local/apps/matlab/matlab2009b/bin/engopts.sh ~

Edit the copy you've made (~/engopts.sh) changing g95 to gfortran. You'll then need to tell mex to use this file. The folowing lines, run inside matlab, copy a fortran demo file and try to compile then run it. You'll need to replace your_home_folder with the name of your home folder (type echo $HOME in Unix to get it).

  cd /tmp
  demofile = [matlabroot '/extern/examples/eng_mat/fengdemo.F'];   
  copyfile(demofile, '.');
  optsfile = 'your_home_folder/engopts.sh';
  mex('-f', optsfile, 'fengdemo.F');
  !./fengdemo

This should work on CUED's central system.

Using mex with fortran from outside Matlab

Having copied the files over as in the previous section, the following should work in unix.

   cd /tmp
   mex -f ~/engopts.sh fengdemo.F 

but again you won't be able to run the resulting program until you've set LD_LIBRARY_PATH as above.