MATLAB Compiler (Mex and Mcc)

From CsWiki
Jump to: navigation, search

The Matlab compiler allows you to compiler either C code written for Matlab or Matlab script files.

Compiling your code will make it run faster and also will not use up a licence for every toolbox invoked in your code.

Mex

Mex allows you to compile C code written especially for Matlab and run it inside the Matlab environment as if it were a builtin Matlab function.

In order to write C code appropriate for Mex, follow the Mex File Guide.

The following session illustrates how to compile and run C code files using Mex while inside Matlab:

Matlab mex85.jpg

Mcc

Mcc allows you to compile and run Matlab script files from the Linux command prompt. These binaries do not require Matlab licences at run time.

The following is an example of how to compile and run a Matlab script fuunction:

setenv MATROOT /usr/local/matlab8/current
setenv LD_LIBRARY_PATH ${MATROOT}/runtime/glnxa64:${MATROOT}/bin/glnxa64:${MATROOT}/sys/java/jre/glnxa64/jre/lib/amd64/server
mcc -m /usr/local/matlab8/current/extern/examples/compiler/magicsquare.m

Ignore any warning messages!!!

./magicsquare 6                                                    
m =
   35     1     6    26    19    24
    3    32     7    21    23    25
   31     9     2    22    27    20
    8    28    33    17    10    15
   30     5    34    12    14    16
    4    36    29    13    18    11

For more details on using the compiler, see the Matlab Compiler Guide.