[Vision Group Logo]

The Matlab Compiler

OK. It is possible to compile Matlab programs into C. This is done using the matlab compiler. I believe not all Matlab programs, such as those that use objects can be compiled. Once compiled into C, a standalone executable can built. The School has 1 licence for this. Once used, the licence remains with the user for 30mins after it was last used, before it is freed and can then be used by another user.

Using mcc on linux

To use mcc a number of environment variables need to be set. I am a bash user. In my ~/.bashrc file, source ~/.mcc_rc which contains the following:
export MCRROOT=/usr/local/matlab-14SP2

export MATLABROOT=/usr/local/matlab-14SP2

export
LD_LIBRARY_PATH=$MATLABROOT/sys/opengl/lib/glnx86:$MATLABROOT/bin/glnx86:$MATLABROOT/sys/os/glnx86/:$MATLABROOT/sys/java/jre/glnx86/jre1.5.0/lib/i386/client/:$MATLABROOT/sys/java/jre/glnx86/jre1.5.0/lib/i386/:$LD_LIBRARY_PATH

export XAPPLRESDIR=/usr/local/matlab-14SP2/X11/app-defaults

You can compile a function in a .m file to a standalone executable with, for example:
mcc -m magicsquare.m
function magicsquare(n)
% MAGICSQUARE generates a magic square matrix of the size specified
% by the input parameter n.
% Copyright 2003 The MathWorks, Inc.

if (ischar(n))
    n=str2num(n);
end
magic(n)
Now the executable can be used on the command line, for example:
./magicsquare 5

ans =
   17    24     1     8    15
   23     5     7    14    16
    4     6    13    20    22
   10    12    19    21     3
   11    18    25     2     9
See man mcc for further info, such as adding include paths, etc...

Possible problems

The mcc-cache dir may need to be removed each time you wish to compile!
rm -r ~/.matlab/R14/mcc.cache

A standalone executable still uses a set of shared libraries. The LD_LIBRARY_PATH says where to find these.

JWH repports issues with using threads if the LD_LIBRARY_PATH is set as suggested. (this may be fixed in version 14.1) Specifically, using /usr/local/matlab-14/sys/os/glnx86/libgcc_s.so.1 causes the problem to abort whenever a thread tries to exit. jwh got around this by using a wrapper to preload a working version of this library:

cat ~/bin/matlabrun
   #!/bin/sh
   export LD_PRELOAD=/lib/libgcc_s.so.1
   exec $*

What do I type to find out what licences are in use?

/usr/local/matlab-14.1/etc/lmstat -a 

Any more tips?

Let me know if you have anything to add to this page. - chrisn

Vision Group Homepage