Create a .NET Application with MATLAB Code

This example shows how to transform a MATLAB® function into a .NET assembly and integrate it into an application. The example compiles a MATLAB function, makesquare, which computes a magic square, into a .NET assembly named MagicSquareComp, which contains the MLTestClass class and other files needed to deploy your application.

Create a .NET Assembly

To create a .NET assembly from your MATLAB function,

  1. In MATLAB, create the function that you want to deploy as a shared library.

    This example uses the sample function, called makesquare.m, included in the matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\MagicSquareExample\MagicSquareComp folder, where matlabroot represents the name of your MATLAB installation folder.

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

    Run the example in MATLAB.

    makesquare(5)
    
    ans =
    
        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
  2. Open the Library Compiler app.

    1. On the toolstrip, select the Apps tab.

    2. Click the arrow at the far right of the tab to open the apps gallery.

    3. Click Library Compiler.

      Note

      You can also call the libraryCompiler command.

  3. In the Type section of the toolstrip, select .NET Assembly from the list.

    Note

    If the Type section of the toolstrip is collapsed, you can expand it by clicking the down arrow.

  4. Specify the MATLAB functions that you want to deploy.

    1. In the Exported Functions section of the toolstrip, click the plus (+) button.

    2. In the file explorer that opens, locate and select the makesquare.m file.

    3. Click Open to select the file and close the file explorer.

      The Library Compiler app adds makesquare.m to the list of files and a minus button appears under the plus button. The Library Compiler app uses the name of the file as the name of the deployment project file (.prj), shown in the title bar, and as the name of the assembly, shown in the first field of the Library Information area. The project file saves all of the deployment settings so that you can re-open the project.

  5. In the Packaging Options section of the toolstrip, verify that the Runtime downloaded from web check box is selected.

    This option creates an application installer that automatically downloads the MATLAB Runtime and installs it along with the deployed add-in.

  6. In the top field of Library Information, replace makesquare with MagicSquareComp.

  7. In the Class Name column of the class browser, replace Class1 with MLTestClass.

  8. Click Save to save the project.

  9. Click Package.

    The Package window opens while the library is being generated. Select the Open output folder when process completes check box. The packaging process generates a self-extracting file that automatically registers the DLL and unpacks all deployable deliverables.

  10. When the deployment process is complete, a file explorer opens and displays the generated output.

    It should contain:

    • for_redistribution — A folder containing the installer to distribute the generated assembly

    • for_testing — A folder containing the raw files generated by the compiler

    • for_redistribution_files_only — A folder containing only the files needed to redistribute the assembly

    • PackagingLog.txt — A log file generated by the compiler

  11. Click Close on the Package window.

Build a .NET Application

To build a .NET application using the assembly you just created,

  1. Install the .NET assembly and the MATLAB Runtime.

    The assembly and the MATLAB Runtime are both installed by the Library Compiler app. The installer is located in the for_redistribution folder of your deployment project. The installer places the .NET assembly on your computer and automatically installs the MATLAB Runtime from the Web, if you do not already have the MATLAB Runtime installed on your system.

    You can also download the MATLAB Runtime installer from http://www.mathworks.com/products/compiler/mcr. The generated shared libraries and support files are located in the MATLAB deployment project's for_testing folder.

  2. Open Microsoft® Visual Studio®.

  3. Create a new project.

    For this example create a C# Console Application called MainApp.

  4. Create a reference to your assembly file MagicSquareComp.dll.

    The assembly is located in the application folder created where you installed the component.

  5. Create a reference to the MWArray API. The location of the API within MATLAB Runtime is:

    C:\Program Files\MATLAB\MATLAB Runtime\v##\toolbox\dotnetbuilder\bin\win64\version\MWArray.dll

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

  7. Copy the following C# code into the project and save it.

    // Make .NET Namespaces available to your generated component.
    using System;
    
    using MagicSquareComp;
    using MathWorks.MATLAB.NET.Arrays;
    
    
    // Initialize your classes before you use them.
    namespace MainApp
    {
      class Program
      {
        static void Main(string[] args)
        {
          MLTestClass obj = null;
          MWNumericArray input = null;
          MWNumericArray output = null;
          MWArray[] result = null;
    
          // Because class instantiation and method invocation make their exceptions at run time,
          // you should enclose your code in a try-catch block to handle errors.
          try
          {
            // Instantiate your component class.
            obj = new MLTestClass();
    
            // Invoke your component.
            input = 5;
            result = obj.makesquare(1, input);
    
            // Extract the Magic Square you created from the first index of result
            output = (MWNumericArray)result[0];
            
            // print the output.
            Console.WriteLine(output);
          }
          catch
          {
            throw;
          }
       }
      }
    }
  8. After you finish writing your code, build and run it with Microsoft Visual Studio.

Was this topic helpful?