How to use the MATLAB Compiler?
(to generate standalone executables)
Different versions of MATLAB may require slightly different procedures. Make sure to use the instructions that correspond to the compiler available on the machine you intend to use to generate standalone executables.
To find out what version compiler you are using, type:
>> mcc -? % The version number is reported near the top of the doc.
If you plan to use the SCV Linux Cluster, it currently runs MATLAB version 6.5 with Compiler version 3.
The following is for MATLAB Compiler Version 3:
>> mcc -m myexample -B sgl % compile myexample.m
function myexample
x = 1:8;
y = x.^2;
plot(x,y)
disp('Will pause for 30 seconds ...')
pause(30)
disp('Done')
exit
The above command compiles myexample.m, which contains MATLAB graphics call, and hence the need for “-B sgl”. For C++, use “-B sglcpp” instead. The executable is myexample (for Linux) or myexample.exe (for Windows), along with files like myexample.c and myexample.h, etc.
To run the standalone executable outside of the MATLAB environment
the following steps must be taken:
For Windows machines:
To prepare a package to send to the target machine for processing:
- The executable, e.g., myexample.exe
- MGLInstaller.exe (this consists of the installation script and math and graphics libs)
- This file resides in $MATLABtoolboxcompilerdeploywin32
where $MATLAB is where MATLAB is installed, for example, c:MATLAB6p5. - This file is needed only once on the same target machine. So, if you have multiple applications, there is no need to send this file to the target machine the second time around.
- This file resides in $MATLABtoolboxcompilerdeploywin32
On the target Windows machine, you need to do the following prior to running jobs:
- Save the above files in a directory of your choice — lets say c:mypath
- Double click on mglinstaller.exe to extract matlab library files.
- The installer will ask you where to extract files into; it will ask if the current dir is OK. The answer would be yes if you only have one standalone application. Otherwise, you may want to install the MATLAB libraries in another “neutral” location as this only need be installed once. Additional standalones can share the same library.
- Finally, you need to set the path to point to this dir:
- Right-click the “My Computer” icon
- Select “Properties”
- Select “Advanced”
- Select “Environment Variables”
- Do one or the other:
- If you are the owner of the machine
- In the second pane, highlight the line “Path”
- Select “edit” (in the same 2nd pane)
- Append ; c:mypathbinwin32 (Don’t forget the “;”) at the end of the line
- Click “OK”
- If you are a user
- In the first pane, select “new”
- Enter “Path” for “Variable”
- Enter c:mypath for “Variable Value”
- Click “OK”
- If you are the owner of the machine
Now, the system is ready to run your standalone executable.
For Linux machines:
- The executable, e.g., myexample
- mglinstaller
On SCV’s Linux CLuster, this file resides in /usr/local/apps/matlab-6.5p5/extern/lib/glnx86 - FigureMenuBar.fig and FigureToolBar.fig
(You need to copy the above two files if your application contains graphics. On SCV’s Linux Cluster, these files reside in
/usr/local/apps/matlab-6.5p5/extern/include)
On the target Linux machine:
- Save myexample, mglinstaller, and the two .fig files in, say, /home/mypath
- Install the MATLAB Graphics Library by running mglinstaller
This results in two directories: toolbox and bin to be created in /home/mypath. You will also need to update the library path as shown below. - Update dynamic library path
(enter it in your .cshrc, or comparable, script file)setenv LD_LIBRARY_PATH <mgl_root›/bin/glnx86:${LD_LIBRARY_PATH}where <mgl_root› is the root directory of mglinstaller. Don’t forget to source .cshrc to rehash it.
Now, the system is ready to run your standalone executable.
On the SCV Linux Cluster, Step 2 above need not be carried out as it is already provided by the system. However, Step 3 must still be followed by you. Here is what you need to add to your .cshrc file
% setenv LD_LIBRARY_PATH /usr/local/matlab/standalone/bin/glnx86:$LD_LIBRARY_PATH
On the SCV Linux Cluster, if you want to see whether your standalone takes up any license, you can issue the following command:
cootie % /usr/local/flexlm/bin/lmstat -c /usr/local/apps/matlab-6.5p5/etc/license.dat -a | grep USERID
where USERID is your userID. Normally, if you are running MATLAB, at least one license (MATLAB license) is charged to your userID. More if you have used simulink, compiler, etc. If you are running the standalone without checking out any MATLAB licenses, the above command should return with no license checked out.
See MATLAB Compiler User’s Guide
![]()



