Wednesday, May 7, 2014

Michael Hirsch's Blog Post on Compiling Matlab code to run on any PC

Compiling Matlab code to run on any PC

August 8, 2013 at 13:01
The Matlab Compiler allows you to compile your existing .m Matlab script to run as an executable–e.g. on another PC that doesn’t have Matlab installed. Typically you will compile for the same operating system as you have on your Matlab PC (it might be possible to cross-compile, but I have NOT checked this). You can also output code suitable for compilation in another compiler–I’ll leave it to you to read the mcc documentation for now.
There are two caveats:
1) You must have a Matlab Compiler license on the PC you’re using for Matlab–check this by typing in Matlab:
which must return a 1. If it’s 0, you don’t the Compiler license (you or your institution would have to buy a license)
2) The computer where you intend to run the compiled program must have the no-cost Matlab Compiler Runtime (MCR) installed–else you’ll get errors like:
error while loading shared libraries: cannot open shared object file: No such file or directory
Well, that’s the error I get in Linux.
To install the Matlab Compiler Runtime (do this on your Matlab PC as well as obviously the PC where you want to run the compiled Matlab code, go to this website:
On Linux, just before finishing the installation you will be told to set environment variables LD_LIBRARY_PATH and XAPPLRESDIR with a long string of text. Here’s how to do that
  1. gedit matlabvariables
  2. copy and paste the text into an export command like this:
    ## Matlab compiler Runtime (MCR)
    export LD_LIBRARY_PATH=/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/bin/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/sys/java/jre/glnxa64/jre/lib/amd64:$LD_LIBRARY_PATH
    export XAPPLRESDIR=/usr/local/MATLAB/MATLAB_Compiler_Runtime/v81/X11/app-defaults
  3. chmod +x matlabvariables
Caution: The above .bashrc is only for Matlab R2013a on my PC, maybe your PC is different–cut and paste the text from your installation window.
So before running your MCR program, first run ./matlabvariables, which sets the appropriate variables when you need them on the PC you’re running the MCR code on.
Note: don’t just paste the export statements into .bashrc, because they can interfere with other programs such as nmap–just use them when needed.
Now we write a test program by saving the following test into a file called MyMCC.m
function Y = MyMCC
X = 0:0.01:2*3.14;
Y = sin(X);
title('Test of MCR')
display('wow I ran an MCR program!')

Then type in Matlab console:
mcc -m -v -w enable -R -startmsg -R -completemsg MyMCC.m
When that completes, open Terminal and in your MyMcc.m directory you’ll see an executable file MyMCC.
Type in Terminal ./MyMCC and you’ll see:

Initializing MATLAB Compiler Runtime version 8.1
wow I ran an MCR program!

along with a typical Matlab plot window showing a sine wave. Just close the plot window to end the execution of your program.

1 comment:

  1. Consider looking into deploytool as well which does a very good job of setting up the runtime environment via a bash script.