| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB Compiler |
| Contents | Index |
| Learn more about MATLAB Compiler |
| On this page… |
|---|
The examples for MATLAB Compiler are in matlabroot\extern\examples\compiler. For matlabroot, substitute the MATLAB root folder on your system. Type matlabroot to see this folder name.
The Magic Square example in this section shows you how to:
Use MATLAB Compiler to create and package a simple standalone application that compiles an M-file, magicsquare.m
Access the examples provided with MATLAB Compiler.
Note If you are using a non-Windows operating system, "console applications" are referred to as "standalone applications". |
Tip To set examplesDir to the path matlabroot\extern\examples\compiler, click here. |
The examples use the deploytool GUI, a graphical front-end interface to MATLAB Compiler software. You can perform these tasks using the command-line interface to MATLAB Compiler software. See Using the Command Line (mcc) to Create Standalone Applications and Shared Libraries and the mcc reference page for complete reference information.
The MATLAB programmer performs the tasks described in the following table.

Key Tasks for the MATLAB Programmer
| Task | Reference |
|---|---|
| Start the product. | Starting the Deployment Tool |
| Prepare to run the example by copying the MATLAB example files into a work folder. | Copying the Example Files |
| Test the M-code to ensure that it is suitable for deployment. | Testing the M-File You Want To Deploy |
| Create a standalone application or shared library (encapsulating your M-code in a C or C++ class) by running the Build function in deploytool. | Creating a Deployable Standalone Application or Shared Library |
| Run the Packaging Tool to bundle your standalone application or shared library with the additional files you selected. | Packaging Your Deployment Application (Optional) |
| Copy the output from the Packaging Tool (the distrib folder). | Copy the Package You Created (Optional) |
The Magic Square example shows how to create a standalone application, or shared library (magicsquare), containing the magic class and other files for application deployment. The class encapsulates a MATLAB function which computes a magic square.
The client standalone application or shared library converts the array returned by the function to a native array and displays it on the screen. When you run the magicsquare application from the command line, you pass the dimension for the magic square as a command-line argument
Note The examples for the MATLAB Compiler product reside in matlabroot\extern\examples\compiler. This example assumes that the work folder is on drive D:. |
You can access the MATLAB Compiler product through the Deployment Tool GUI (deploytool). Alternately, you can use the mcc function of MATLAB Compiler. deploytool is the GUI front end for mcc, the command that executes MATLAB Compiler.
This tutorial uses deploytool. If you want to use mcc, see Using the Command Line (mcc) to Create Standalone Applications and Shared Libraries . Also see the mcc reference page for complete reference information.
To start the Deployment Tool by perform the following steps:
Start MATLAB.
Type deploytool at the MATLAB command prompt. The deploytool GUI opens.
Prepare to run the example by copying needed files into your work area as follows:
Navigate to matlabroot\extern\examples\compiler. matlabroot is the MATLAB root folder (where you installed MATLAB). To find the value of this variable on your system, type matlabroot at a MATLAB command prompt.
Create a work folder named Work (D:\Work). Create a subfolder in your Work folder and name it MagicExample (D:\Work\MagicExample). Avoid using spaces in your folder names, if possible.
Copy magicsquare.m from matlabroot\extern\examples\compiler to D:\Work\MagicExample.
To run your resulting client standalone application or shared library, verify that MATLAB can find it. Use the File > Set Path option in MATLAB to add the D:\Work\MagicExample folder to the MATLAB search path.
At the MATLAB command prompt, change your working folder to D:\Work\MagicExample.
In this example, you test an M-file (magicsquare.m) containing the predefined MATLAB function magic. Testing the file provides a baseline to compare to the results of the function as a deployable standalone application or shared library.
Using MATLAB, locate and open magicsquare.m. This file has the following:
function m = magicsquare(n)
%MAGICSQUARE generates a magic square matrix of the size
% specified by the input parameter n.
% Copyright 2003-2007 The MathWorks, Inc.
if ischar(n)
n=str2num(n);
end
m = magic(n)
At the MATLAB command prompt, enter magicsquare(5), and view the results. The output appears as follows:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
You create a deployable standalone application or shared library by using the Deployment Tool GUI to build a wrapper. This wrapper encloses the sample M-code discussed in Testing the M-File You Want To Deploy. To run the Magic Square example, use the following information:
| Project Name | MagicExample |
| File to compile | magicsquare.m |
Create a deployment project. A project is a collection of files you bundle together under a project file name (.prj file) for your convenience in the Deployment Tool. Using a project makes it easy for you to build and run an application many times—effectively testing it—until it is ready for deployment.
Type the name of your project in the Name field.
Enter the location of the project in the Location field. Alternately, navigate to the location.
Select the target for the deployment project from the Target drop-down menu.
Click OK.
On the Build tab, add what you want to compile, and any supporting files, to the project.
Do the following, depending on the type of application you are building:
If you are building a C or C++ application, click Add files
Add any supporting files. For example, you can add the following files, as appropriate for your project:
Functions called using eval (or variants of eval)
Functions not on the MATLAB path
Code you want to remain private
Code from other programs that you want to compile and link into the main file
If you want to include additional files, in the Shared Resources and Helper Files area, click Add files/directories. Click Open to select the file or files.
When you complete your changes, click the Build button
(
).
Packaging is bundling the standalone application or shared libraries with additional files for end users. Perform this step using the Package tab of deploytool. Alternately, copy the contents of the distrib folder and the MCR Installer to a local folder of your choice.
On the Package tab, add the MATLAB Compiler Runtime (the MCR) by clicking Add MCR.
Next, add others files useful for end users. The readme.txt file contains important information about others files useful for end users. To package additional files or folders, click Add file/directories, select the file or folder you want to package, and click Open.
In the Deployment Tool, click the Packaging button
(
).
After packaging, the package resides in the distrib subfolder. On Windows®, the package is a self-extracting executable. On platforms other than Windows, it is a .zip file. Verify that the contents of the distrib folder contains the files you specified.
Copy the package that you created from the distrib folder to the local folder of your choice or send them to the C/C++ programmer, if applicable.
Instead of the GUI, you can use the mcc command to run MATLAB Compiler. The following table shows sample commands to create a standalone application or a shared library using mcc at the operating system prompt.
The C or C++ programmer performs these tasks.
Key Tasks for the C or C++ Programmer
| Task | Reference |
|---|---|
| Ensure that you have the needed files from the MATLAB Programmer before proceeding. | Gathering Files Necessary for Deployment |
| Distribute the files. | Distribute to End Users |
| Install the MCR on target computers by running the MCR Installer. Update system paths on UNIX systems. | Install the MCR on Target Computers Without MATLAB and Update System Paths |
| Ensure that the final standalone application or library executes reliably in the end-user environment. | Build and Test |
Before beginning, verify that you have access to the following files, packaged by the MATLAB Programmer in Copy the Package You Created (Optional). End users who do not have a copy of MATLAB installed need the following:
MCR Installer. For locations of all MCR Installers, run the mcrinstaller command.
readme.txt file
See Packaging Your Deployment Application (Optional) for more information about these files.
If the MATLAB programmer packages the standalone or library (see Packaging Your Deployment Application (Optional)), paste the package in a folder on the target machine, and run it. If you are using a .zip file bundled with WinZip, unzip and extract the contents to the target machine.
Note If you are using a non-Windows operating system, "console applications" are referred to as "standalone applications". |
On target computers without MATLAB, install the MCR, if it is not already present on the development machine:
What Is the MATLAB Compiler Runtime (MCR) and How Do I Get
It?
Replacement of MCRInstaller.zip and BUILDMCR Functionality. In past releases, you included MCRInstaller.zip in your packaged application (created by running the buildmcr command). Now, you run the following files, which trigger self-extracting archives, that replace the functionality previously provided by MCRInstaller.zip. These files ship with MATLAB Compiler. To get information on the where you can find the MCR Installer, run the command mcrinstaller.
Build and test the standalone application or shared library as you would any application in your environment. After you create and distribute the initial application, you continue to enhance it.
![]() | Before You Use MATLAB Compiler | For More Information | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |