Generate MWArray .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.
This example uses the legacy MWArray API. To create a .NET assembly using the more modern MATLAB Data API, see Deploy MATLAB Function to .NET Application Using MATLAB Data API for .NET. To select the right deployment option for your project, see Choose .NET Deployment Option.
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. For more details on developing applications with .NET, see Set Up .NET Development Environment.
End users must have an installation of MATLAB Runtime to run the application. For details, see Download and Install 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 9Create .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 with MWArray API.
If you have not already created the file
makesquare.m, copy the example file located in.matlabroot\toolbox\dotnetbuilder\Examples\VS15\NET\MagicSquareExample\MagicSquareCompcopyfile(fullfile(matlabroot,'toolbox','dotnetbuilder','Examples',... 'VS15','NET','MagicSquareExample','MagicSquareComp','makesquare.m'));
Save the following code in a sample file named
makesquareSample1.m:x = 5; y = makesquare(x);
Build the .NET assembly using the
compiler.build.dotNETAssemblyfunction. 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.buildcommand by using name-value arguments. For details, seecompiler.build.dotNETAssembly.The
compiler.build.ResultsobjectbuildResultscontains information on the build type, generated files, included support packages, and build options.The build function generates files within a folder named
MagicSquareCompdotNETAssemblyin your current working directory. For information on the files generated, see Files Generated After Packaging MATLAB Functions.Note
The generated assembly does not include MATLAB Runtime or an installer. To create an installer using the
buildResultsobject, seecompiler.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\
Open Microsoft Visual Studio and create a C# Console App (.NET Framework) called
MainApp.Remove any source code files that were created within your project, if necessary.
Add the sample C# application code
makesquareSample1.csthat was generated in thesamplesfolder 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:
Uses a
try-catchblock to handle exceptionsCreates an
MWNumericArrayarray to store the input dataInstantiates the
MagicSquareClassobjectresultsCalls the
makesquaremethod, where the first parameter specifies the number of output arguments and the following parameters are passed to the function in order as input argumentsWrites the function output to the console
In Visual Studio, add a reference to your assembly file
MagicSquareComp.dlllocated in the folder where you generated or installed the assembly.Add a reference to the
MWArrayAPI.If MATLAB is installed on your system matlabroot\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dllIf MATLAB Runtime is installed on your system <MATLAB_RUNTIME_INSTALL_DIR>\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dllGo to Build, then Configuration Manager, and change the platform from Any CPU to x64.
After you finish adding your code and references, build the application with Visual Studio.
The build process generates an executable named
makesquareSample1.exe.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