Compile Matlab .m code executable

Matlab “.m” code can be compiled to an executable so that Matlab isn’t needed to run compiled Matlab code. The free Matlab Compiler Runtime is needed on the non-Matlab computer to run the binary executable from Matlab Compiler.

Caveats

Matlab Compiler does not in general speedup your Matlab code execution. Keep in mind that any executable binary generated by any means from any coding language can be disassembled to reverse-engineer the underlying code, so distributing an executable instead of “.m” code does not completely secure IP from theft. Matlab Compiler is said to use AES encryption but since the method is non-public, there could be unknown vulnerabilities.

Configuration

The Matlab Compiler Runtime (MCR) version on each computer running the executable must match the Matlab version of the compiling Matlab. This information must be shared with executable users. The compiling computer must be on the same operating system as the MCR running computers.

  1. Compiling computer: ensure Matlab Compiler is installed:

    assert(license('test', 'compiler') == 1)
    
  2. non-Matlab computers: Install MCR

Example

  1. create test program by saving into a file mymcc.m

    function Y = mymcc()
    
    X = 0:0.01:2*3.14;
    Y = sin(X);
    plot(X,Y)
    title('Test of MCR')
    xlabel('x')
    ylabel('y')
    disp('I ran an MCR program!')
    
    end
    
  2. compile .m file in Matlab:

    mcc -m -v mymcc.m
    
  3. Run compiled Matlab program without Matlab

    ./run_mymcc.sh mymcc
    

    which displays

    I ran an MCR program!

    along with a typical Matlab plot window showing a sine wave.

  4. Close the plot window to end the execution of your program.

Notes

Matlab Compiler compiles existing .m Matlab script to run as an executable on another PC without Matlab.

GNU Octave

GNU Octave does not currently have the ability to compile .m files as Matlab does. Another way to share .m code with non-Matlab users is to have them install free GNU Octave, which runs almost all Matlab .m code that doesn’t use obscure specialized proprietary Matlab toolboxes.

Troubleshooting

See the readme.txt created with the mcc command above.