| Contents | Index |
| On this page… |
|---|
The MATLAB programmer performs the following tasks.
MATLAB Programmer
| Role | Knowledge Base | Responsibilities |
|---|---|---|
|
|
|
|
Key Tasks for the MATLAB Programmer
| Task | Reference |
|---|---|
| Test the MATLAB code to ensure that it is suitable for deployment. | magicsquare Testing |
| Create a standalone application or shared library by running the deploytool. | Deployable Standalone or Shared Library Creation |
| Run the Packaging Tool to bundle your standalone application or shared library with any additional files you select. | Packaging (Optional) |
| Copy the output from the Packaging Tool (the distrib folder) and hand off to the C/C++ developer. | Package Copying (Optional) |
In this example, you test a MATLAB file (magicsquare.m) containing the predefined MATLAB function magic, in order to have a baseline to compare to the results of the function when it is deployed as a standalone application or shared library.
Using MATLAB, locate and open magicsquare.m. This file should appear similar to the following:
function m = magicsquare(n)
%MAGICSQUARE generates a magic square matrix of the size
% specified by the input parameter n.
% Copyright 2003-2012 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
| If you want to... | See... |
|---|---|
| MATLAB Code Deployment |
You create a deployable standalone application or shared library by using the Deployment Tool GUI to build a wrapper. This wrapper encloses the sample MATLAB code discussed in magicsquare Testing.
Using MATLAB Compiler, you have the choice to compile your MATLAB code to these four targets:
MATLAB Compiler Supported Compilation Targets
| Compilation Target | For information about when to compile to this target.... |
|---|---|
| Standalone Application | When to Create a Standalone Application |
| Windows Console Application | When to Create a Standalone Application What's the Difference Between a Windows Standalone Application and a Console/Standalone Application? |
| C Shared Library | When to Create a Shared Library C Shared Libraries |
| C++ Shared Library | When to Create a Shared Library C++ Shared Libraries |
Use the following information when creating your component as you work through this example:
| Project Name | MagicExample |
| File to compile | magicsquare.m |
Start MATLAB, if you have not done so already.
Type deploytool at the command prompt, and press Enter. The Deployment Project dialog box opens.
The Deployment Project Dialog Box

Create a deployment project using the Deployment Project dialog box:
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 Type drop-down menu.
Click OK.
On the Build tab:
If you are building a standalone target, click Add main file to open the Add Files dialog box..
Click Open to select the file or files.
If you are building a shared library target, click Add files to open the Add Files dialog box.
Click Open to select the file or files.
You may optionally add supporting files. For examples of these files, see the deploytool Help. To add these files, in the Shared Resources and Helper Files area:
Click Add files/directories
Click Open to select the file or files.
Note If you are using a non-Windows operating system, "console applications" are referred to as "standalone applications". For more information about the differences between standalones and Windows standalone, see What's the Difference Between a Windows Standalone Application and a Console/Standalone Application? in this User's Guide. |
When you complete your changes, click the Build button
(
). When the build finishes,
click Close to dismiss the dialog box.
After you build your standalone application or shared library with the Deployment Tool, you have the following in the src and distrib subdirectories of your project directory:
| These Subdirectories of the Project Directory: | Contain these files: |
|---|---|
| src | If you built a standalone application:
|
| distrib | If you built a standalone application:
|
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 (MCR). To do so, click Add MCR, and choose one of the two options described in the following table.
| Option | What Does This Option Do? | When Should I Use This Option? |
|---|---|---|
| Embed the MCR in the package | This option physically copies the MCR Installer file into the package you create. |
|
| Invoke the MCR from a network location | This option lets you add a link to an MCR Installer residing
on a local area network, allowing you to invoke the installer over
the network, as opposed to copying the installer physically into the
deployable package. This option sets up a script to install the MCR from a specified network location, saving time and resources when deploying applications. |
If you choose this option, modify the location of the MCR Installer, if needed. To do so, select the Preferences link in this dialog box, or change the Compiler option in your MATLAB Preferences. |
For more information about the role the MCR plays in the deployment process, see Distribute MATLAB Code Using the MATLAB Compiler Runtime (MCR).
Next, add others files you feel may be useful to 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
(
).
For Windows, the package is a self-extracting executable. On platforms other than Windows, the package is delivered as 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.
![]() | The Magic Square Example | Integrating Your Shared Library With a C/C++ Application | ![]() |

Learn how to build standalone executables and C/C++ shared libraries from MATLAB code.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |