How to use the MATLAB Compiler?

For MATLAB Compiler Version 2
  • Make a C translation and a MEX-file for mystuff.m:
    >> mcc -x mystuff
  • Make a C translation and a stand-alone executable for mystuff.m:
    >> mcc -m mystuff
  • Make a C++ translation and a stand-alone executable for mystuff.m:
    >> mcc -p mystuff
  • Make a C translation and a Simulink S-function for mystuff.m
    (using dynamically sized inputs and outputs):
    >> mcc -S mystuff

A few notes:

  • To use mcc, your m-file must be a function m-file (as opposed to a script m-file, see compiler limitations), e.g.,
           function mystuff
             x = 1:2:9
             y = 2:2:9
    
  • The second example above generates a stand-alone executable called “mystuff”. Before you can run job outside, or in the absence, of MATLAB, you must enter the command

    unix-prompt% setenv LD_LIBRARY_PATH matlab-library-path

    at unix prompt or include it in your .cshrc (or other shells). In the above, matlab-library-path is the full path where the matlab library lives. On SCV’s machines, the path would be
    /usr/local/IT/matlab-5.3.1/extern/lib/sgi64

  • Type
    >> help mcc<.
    at the matlab prompt for additional details and options for mcc.
  • You can execute mcc inside or outside of the MATLAB environment. However, to run mcc outside of MATLAB, due to name conflict with an existing package by the same name, you must provide the full path to avoid using the unintended package. On SCV machines, either

    unix-prompt% /usr/local/IT/matlab-5.3.1/bin/mcc -m mystuff

    or put the following

    alias mcc '/usr/local/IT/matlab-5.3.1/bin/sgi64/mcc'

    in your .aliases file if you don’t plan to use Mathematica’s mcc.

  • If your m-file requires input, like function myfun(n, m), “n” and “m” are treated as strings in the standalone executable. So, you have to do a conversion to use it. In order to have one m-file for both purposes, this is a common way to do it:
    function [m, f] = myfun(n)
    if ischar(n) == 1
      n = str2num(n);   % this statement takes effect for standalone
    else
      n = n;            % this statement exectuted in >>
    end
    m = n * n           % this is one way to send output back (no ";")
    f = n * 1.5;
    disp(f)             % another way to send output back
    

    To run the standalone executable, say, myfun.exe, here is what you’ll do:

    MS Dos > myfun.exe 5
    unix % myfun.exe 5 > outfile   <== redirect m and f to file "outfile"
    

    There is nothing equivalent to the above for output from a standalone executable. Suppressing the “;” or use

    disp
    

    for that purpose. Here is a matlab link on this topic.

See MATLAB Compiler User’s Guide

unk

unk unk unk