Generate .NET Assembly and Build .NET Application - MATLAB & Simulink (original) (raw)

Main Content

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.

Prerequisites

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 compiler.build.dotNETAssembly

Build a .NET assembly using a programmatic approach. Alternatively, if you want to create a .NET assembly using a graphical interface, see Package MATLAB Function Using .NET Assembly Compiler App.

  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.
    copyfile(fullfile(matlabroot,'toolbox','dotnetbuilder','Examples',...
    'VS15','NET','MagicSquareExample','MagicSquareComp','makesquare.m'));
  2. Save the following code in a sample file namedmakesquareSample1.m:
    x = 5;
    y = makesquare(x);
  3. Build the .NET assembly using thecompiler.build.dotNETAssembly function. Use name-value arguments to specify the assembly name, class name, and sample file.
    buildResults = compiler.build.dotNETAssembly('makesquare.m',...
    'AssemblyName','MagicSquareComp',...
    'ClassName','MagicSquareClass',...
    'SampleGenerationFiles','makesquareSample1.m');
    You can specify additional options in the compiler.build command by using name-value arguments. For details, see compiler.build.dotNETAssembly.
    The compiler.build.Results objectbuildResults contains information on the build type, generated files, included support packages, and build options.
    The function generates the following files within a folder namedMagicSquareCompdotNETAssembly 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.
      Note
      The generated assembly does not include MATLAB Runtime or an installer. To create an installer using thebuildResults 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.

Note

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:

matlabroot\toolbox\dotnetbuilder\Examples\VS15\NET\MagicSquareExample\
  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];  
            }  
            Console.WriteLine(yOut);  
        } catch (Exception e) {  
            Console.WriteLine(e);  
        }  
    }  
    /// <summary>  
    /// The main entry point for the application.  
    /// </summary>  
    static void Main(string[] args) {  
        try {  
            Setup();  
        } catch (Exception e) {  
            Console.WriteLine(e);  
            Environment.Exit(1);  
        }  
        try {  
            makesquareSample();  
        } catch (Exception e) {  
            Console.WriteLine(e);  
            Environment.Exit(1);  
        }  
    }  
}  

The program does the following:

  1. In Visual Studio, add a reference to your assembly fileMagicSquareComp.dll located in the folder where you generated or installed the assembly.
  2. Add a reference to the MWArray API.
    If MATLAB is installed on your system matlabroot_\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
  3. Go to Build, then Configuration Manager, and change the platform from to .
  4. After you finish adding your code and references, build the application with Visual Studio.
    The build process generates an executable namedmakesquareSample1.exe.
  5. 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

compiler.build.dotNETAssembly | mcc

Topics