| Contents | Index |
| On this page… |
|---|
MATLAB Programmer
| Role | Knowledge Base | Responsibilities |
|---|---|---|
|
|
|
|
The MATLAB programmer usually performs the following tasks.
Key Tasks for the MATLAB Programmer
| Task | Reference |
|---|---|
| Test the MATLAB code to ensure it is suitable for deployment. | makesquare Testing |
| Create a .NET component by running the deploytool. | Deployable Component Creation |
| Optionally, use the Packaging Tool to wrap all deliverable files in one bundle. | Packaging Your Component (Optional) |
| Copy the output so the .NET programmer can work with it further. | Copying the Package You Created |
In this example, you test a precreated MATLAB file (makesquare.m) containing the predefined MATLAB function magic, in order to have a baseline to compare to the results of the function when it is finally wrappered as a deployable .NET component.
Using MATLAB, locate the makesquare.m file at matlabroot\toolbox\dotnetbuilder\Examples\VS8\NET\MagicSquareExample\MagicSquareComp. This file has the following contents:
function y = makesquare(x) %MAKESQUARE Magic square of size x. % Y = MAKESQUARE(X) returns a magic square of size x. % This file is used as an example for the MATLAB % Builder NE product. % Copyright 2001-2011 The MathWorks, Inc. y = magic(x);
At the MATLAB command prompt, enter makesquare(5), and view the results. The output should appear 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 .NET component by using the Deployment Tool GUI to build a .NET class that wraps around the sample MATLAB code discussed in makesquare Testing.
Use the following information when creating your component as you work through this example:
| Project Name | makeSqr |
| Class Name | MLTestClass |
| File to compile | makesquare.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 Target drop-down menu.
Click OK.
On the Build tab:
If you are building a COM application, click Add files.
Click Open to select the file or files.
If you are building a .NET application, click Add class. Type the name of the class in the Class Name field, designated by the letter c:
![]()
For this class, add MATLAB files you want to compile by clicking Add files then click Open to select the file or files. To add another class, click Add class.
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 building a COM component (not a .NET component) and you don't have administrator privileges, you can select the Register the resulting component for you only on the development machine option in the Settings dialog of the Deployment Tool (in the Advanced tab) or use the mcc -u option. |
When you complete your changes, click the Build button
(
).
After you build your add-in 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 |
| |
| distrib |
|
| If you want to... | See... |
|---|---|
| MATLAB Code Deployment |
Bundling the .NET component with additional files you can distribute to users is called packaging. You perform this step using the packaging function of deploytool. If you are creating a shared component and want to include additional code with the component, you must perform this step.
The package process zips the following files into a single self-extracting executable, componentName.exe:
componentName.dll
componentNameNative.dll
Note See .NET Remoting for more information on using the native .NET API. |
componentName.xml
componentName.pdb (if the Debug option is selected)
MCR Installer (if the Include MCR option is selected)
_install.bat (script run by the self-extracting executable)
Tip Instead of performing the steps below, you can alternately copy 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 Installing 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.
The MATLAB Compiler Runtime (MCR) is an execution engine made up of the same shared libraries MATLAB uses to enable the execution of MATLAB files on systems without an installed version of MATLAB.
In order to deploy a component, you package the MCR along with it. Before you utilize the MCR on a system without MATLAB, run the MCR Installer.
The installer does the following:
Installs the MCR (if not already installed on the target machine)
Installs the component assembly in the folder from which the installer is run
Copies the MWArray assembly to the Global Assembly Cache (GAC), as part of installing the MCR
The builder creates a single MCR instance for each Builder class in an application. All class instances share a single MATLAB workspace and share global variables in the MATLAB files used to build the component. This MCR instance is reused and shared among all subsequent class instances within the component. Such reuse and sharing results in more efficient memory usage and elimination of MCR startup cost in each subsequent class instantiation.
| If you want to... | See... |
|---|---|
| Build (compile) a component from your MATLAB code | Deployable Component Creation |
| Integrate your component into a production environment | Integrating Your Component into a .NET Class Using Microsoft Visual Studio |
| MATLAB Code Deployment |
Copy the package that you created to the local folder of your choice, or send them directly to the .NET developer.
![]() | The Magic Square Example | Magic Square Example: Integrating Your .NET Component In a C# Application | ![]() |

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