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.

Prerequisites

  • 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.

Files

MATLAB Functionmatlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\MagicSquareExample\MagicSquareComp\makesquare.m
MWArray API Referencematlabroot\help\dotnetbuilder\MWArrayAPI

Create Function in MATLAB

In MATLAB, examine the MATLAB code that you want to package. For this example, open makesquare.m.

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

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

  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.

    libraryCompiler

    Compiler tab with the Library Compiler app open

  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.

      Note

      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.

  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 Driver File

You can use any MATLAB file in the project to generate sample .NET driver files. Although .NET driver files are not necessary to create an assembly, you can use them to implement the generated assembly into 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.

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.

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

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. Save the path to the file makesquare.m located in matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\MagicSquareExample\MagicSquareComp. For example, if you are using Visual Studio version 15, type:

    appFile = fullfile(matlabroot,'toolbox','dotnetbuilder','Examples', ...
        'VS15','NET','MagicSquareExample','MagicSquareComp','makesquare.m');
  2. Save the following code in a sample file named makesquareSample1.m:

    x = 5;
    y = makesquare(x);

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

    buildResults = compiler.build.dotNETAssembly(appFile, ...
    'AssemblyName','MagicSquareComp', ...
    'ClassName','MagicSquareClass', ...
    'SampleGenerationFiles','makesquareSample1.m');

    The compiler.build.Results object buildResults contains information on the build type, generated files, and build options.

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

    • samples\makesquareSample1.cs — .NET sample driver file.

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

    • 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 the buildResults object, see compiler.package.installer.

    You can specify additional options in the compiler.build command by using one or more of the following name-value arguments:

    • 'AdditionalFiles' — Path to additional files to include in the assembly.

    • 'AssemblyName'— Name of the generated assembly.

    • 'AssemblyVersion'— Version of the generated assembly.

    • 'AutoDetectDataFiles' — Flag to automatically include data files.

    • 'ClassName' — Name of the .NET class.

    • 'DebugBuild' — Flag to enable debug symbols.

    • 'EmbedArchive'— Flag to embed the assembly archive.

    • 'EnableRemoting'— Flag to control the remoting type.

    • 'OutputDir' — Path to the output directory that contains generated files.

    • 'SampleGenerationFiles'— MATLAB sample files used to generate .NET driver files. For more information, see Sample Driver File Creation.

    • 'StrongNameKeyFile'— Path to the encryption key file used to sign the shared assembly.

    • 'Verbose'— Flag to display progress information indicating compiler output during the build process.

Integrate .NET Assembly Into .NET Application

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

  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 generated sample .NET application code makesquareSample1.cs from the for_redistribution_files_only\samples folder to the project.

    The program listing is shown below.

     makesquareSample1.cs

  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 with 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

Note

This example shows how to call the .NET assembly from a sample C# application. To call the assembly from a Visual Basic® application, use the Microsoft Visual Studio project file MagicSquareVBApp.vbproj and Visual Basic file MagicSquareApp.vb located in

matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\MagicSquareExample\MagicSquareVBApp\

See Also

| | |

Related Topics