Main Content

Generate .NET Assembly and Build .NET Application

Supported platform: Windows®

This example shows how to create a .NET assembly from a MATLAB® function and integrate the generated assembly into a .NET application.


  • Verify that you have met all of the MATLAB Compiler SDK™ .NET target requirements. For details, see MATLAB Compiler SDK .NET Target Requirements.

  • Verify that you have Microsoft® Visual Studio® installed.

  • End users must have an installation of MATLAB Runtime to run the application. For details, see Install and Configure MATLAB Runtime.

    For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime.

Create Function in MATLAB

In MATLAB, examine the MATLAB code that you want to package. For this example, create a MATLAB script named makesquare.m.

function y = makesquare(x)
y = magic(x);

At the MATLAB command prompt, enter makesquare(5).

The output is a 5-by-5 matrix.

    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

Create .NET Assembly Using Library Compiler App

Package the function into a .NET assembly using the Library Compiler app. Alternatively, if you want to create a .NET assembly from the MATLAB command window using a programmatic approach, see Create .NET Assembly Using

  1. On the MATLAB Apps tab, on the far right of the Apps section, click the arrow. In Application Deployment, click Library Compiler.

    Alternatively, you can open the Library Compiler app from the MATLAB command prompt.


    Library Compiler App with the .NET Assembly type selected

  2. In the Type section of the toolstrip, click .NET Assembly.

    In the Library Compiler app project window, specify the files of the MATLAB application that you want to deploy.

    1. In the Exported Functions section of the toolstrip, click Add exported function to the project.

    2. In the Add Files window, browse to the example folder, and select the function you want to package. Click Open.

    The function is added to the list of exported function files. Repeat this step to package multiple files in the same application.

    For this example, select the file makesquare.m.

  3. In the Packaging Options section of the toolstrip, decide whether to include the MATLAB Runtime installer in the generated application by selecting one of the options:

    • Runtime downloaded from web — Generate an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application. You can specify the file name of the installer.

    • Runtime included in package — Generate an application that includes the MATLAB Runtime installer. You can specify the file name of the installer.


      The first time you select this option, you are prompted to download the MATLAB Runtime installer.

Specify Assembly File Settings

Next, define the name of your assembly and verify the class mapping for the .m file that you are building into your application.

  1. The Library Name field is automatically populated with makesquare as the name of the assembly. Rename it as MagicSquareComp. The same name is followed through in the implementation of the assembly.

    The Library Name field does not support spaces or special characters.

  2. Verify that the function defined in makesquare.m is mapped into MagicSquareClass. Double-click on the class to change the class name.

Create Sample MATLAB File

You can use any MATLAB file in the project to generate a sample MATLAB file, which is used to generate a .NET sample driver file. Although .NET sample files are not necessary to create an assembly, you can use them as a guide to implement a .NET application in the target language, as shown in Integrate .NET Assembly Into .NET Application.

In the Samples section, select Create New Sample, and click makesquare.m. A MATLAB file opens for you to edit.

% Sample script to demonstrate execution of function y = makesquare(x)
x = 0; % Initialize x here
y = makesquare(x);

Change x = 0 to x = 5, save the file, and return to the Library Compiler app.


You must edit the MATLAB sample file to output your desired result. Generated target language sample files use the same inputs and outputs as the sample MATLAB file.

For more information and limitations, see Sample Driver File Creation.

Customize Application and Its Appearance

In the Library Compiler app, you can customize the installer, customize your application, and add more information about the application.

  • Library information — Information about the deployed application. You can also customize the appearance of the application by changing the application icon and splash screen. The generated installer uses this information to populate the installed application metadata. See Customize the Installer.

  • Additional installer options — Default installation path for the generated installer and custom logo selection. See Change the Installation Path.

  • Files required for your library to run — Additional files required by the generated application to run. These files are included in the generated application installer. See Manage Required Files in Compiler Project.

  • Files installed for your end user — Files that are installed with your application.

    See Specify Files to Install with Application.

  • Additional runtime settings — Platform-specific options for controlling the generated executable. For details, see Additional Runtime Settings.

    Fields in the Library Compiler app for customizing your application

Package the Application

When you are finished selecting your packaging options, save your Library Compiler project and generate the packaged application.

  1. Click Package.

    In the Save Project dialog box, specify the location to save the project.

  2. In the Package dialog box, verify that Open output folder when process completes is selected.

    When the packaging process is complete, examine the generated output in the target folder.

    • Three folders are generated: for_redistribution, for_redistribution_files_only, and for_testing.

      For more information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.

    • The log file PackagingLog.html contains packaging results.

Create .NET Assembly Using

As an alternative to the Library Compiler app, you can create a .NET assembly using a programmatic approach. If you have already created an assembly using the Library Compiler, see Integrate .NET Assembly Into .NET Application.

  1. If you have not already created the file makesquare.m, copy the example file located in matlabroot\toolbox\dotnetbuilder\Examples\VS15\NET\MagicSquareExample\MagicSquareComp.

  2. Save the following code in a sample file named makesquareSample1.m:

    x = 5;
    y = makesquare(x);

  3. Build the .NET assembly using the function. Use name-value arguments to specify the assembly name, class name, and sample file.

    buildResults =,...

    You can specify additional options in the command by using name-value arguments. For details, see

    The object buildResults contains information on the build type, generated files, included support packages, and build options.

    The function generates the following files within a folder named MagicSquareCompdotNETAssembly in your current working directory:

    • samples\makesquareSample1.cs — C# .NET sample file.

    • GettingStarted.html — HTML file that contains steps on compiling .NET driver applications from the command line.

    • includedSupportPackages.txt — Text file that lists all support files included in the assembly.

    • MagicSquareComp.dll — Dynamic-link library file that can be accessed using the mwArray API.

    • MagicSquareComp.xml — XML file that contains documentation for the mwArray assembly.

    • MagicSquareComp_overview.html — HTML file that contains requirements for accessing the assembly and for generating arguments using the mwArray class hierarchy.

    • MagicSquareCompNative.dll — Dynamic-link library file that can be accessed using the native API.

    • MagicSquareCompNative.xml — XML file that contains documentation for the native assembly.

    • MagicSquareCompVersion.cs — C# file that contains version information.

    • mccExcludedFiles.log — Log file that contains a list of any toolbox functions that were not included in the application. For information on non-supported functions, see MATLAB Compiler Limitations.

    • readme.txt — Text file that contains packaging and interface information.

    • requiredMCRProducts.txt — Text file that contains product IDs of products required by MATLAB Runtime to run the application.

    • unresolvedSymbols.txt — Text file that contains information on unresolved symbols.


    The generated assembly does not include MATLAB Runtime or an installer. To create an installer using the buildResults object, see compiler.package.installer.

Integrate .NET Assembly Into .NET Application

After creating your .NET assembly, you can integrate it into any .NET application. This example uses the sample C# application code generated during packaging. You can use this sample .NET application code as a guide to write your own application.


To call the assembly using a more advanced application that takes an input argument, use the C# or Visual Basic® application MagicSquareApp located in the corresponding subfolder of:


  1. Open Microsoft Visual Studio and create a C# Console App (.NET Framework) called MainApp.

  2. Remove any source code files that were created within your project, if necessary.

  3. Add the sample C# application code makesquareSample1.cs that was generated in the samples folder to the project.

    The program listing is shown below.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using MathWorks.MATLAB.NET.Arrays;
    using MathWorks.MATLAB.NET.Utility;
    using MagicSquareComp;
    /// <summary>
    /// Sample driver code that integrates a compiled MATLAB function
    /// generated by MATLAB Compiler SDK
    /// Refer to the MATLAB Compiler SDK documentation for more
    /// information.
    /// </summary>
    class makesquareSample1 {
        static MagicSquareClass MagicSquareClassInstance;
        static void Setup() {
            MagicSquareClassInstance = new MagicSquareClass();
        /// <summary>
        /// Example of using the makesquare function.
        /// </summary>
        public static void makesquareSample() {
            double xInData = 5.0;
            MWNumericArray yOut = null;
            Object[] results = null;
            try {
                MWNumericArray xIn = new MWNumericArray(xInData);
                results = MagicSquareClassInstance.makesquare(1, xIn);
                if (results[0] is MWNumericArray) {
                    yOut = (MWNumericArray) results[0];
            } catch (Exception e) {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args) {
            try {
            } catch (Exception e) {
            try {
            } catch (Exception e) {

    The program does the following:

    • Uses a try-catch block to handle exceptions

    • Creates an MWNumericArray array to store the input data

    • Instantiates the MagicSquareClass object results

    • Calls the makesquare method, where the first parameter specifies the number of output arguments and the following parameters are passed to the function in order as input arguments

    • Writes the function output to the console

  4. In Visual Studio, add a reference to your assembly file MagicSquareComp.dll located in the folder where you generated or installed the assembly.

  5. Add a reference to the MWArray API.

    If MATLAB is installed on your systemmatlabroot\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dll
    If MATLAB Runtime is installed on your system<MATLAB_RUNTIME_INSTALL_DIR>\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dll

  6. Go to Build, then Configuration Manager, and change the platform from Any CPU to x64.

  7. After you finish adding your code and references, build the application with Visual Studio.

    The build process generates an executable named makesquareSample1.exe.

  8. Run the application from Visual Studio, in a command window, or by double-clicking the generated executable.

    The application returns the same output as the sample MATLAB code.

        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

See Also

| | |

Related Topics