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.
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 compiler.build.dotNETAssembly.
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
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.
In the Exported Functions section of the toolstrip, click .
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
.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.
The Library Name field is automatically populated with
makesquare
as the name of the assembly. Rename it asMagicSquareComp
. The same name is followed through in the implementation of the assembly.The Library Name field does not support spaces or special characters.
Verify that the function defined in
makesquare.m
is mapped intoMagicSquareClass
. 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.
Caution
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.
Additional runtime settings — Platform-specific options for controlling the generated executable. For details, see Additional Runtime Settings.
Package the Application
When you are finished selecting your packaging options, save your Library Compiler project and generate the packaged application.
Click Package.
In the Save Project dialog box, specify the location to save the project.
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
, andfor_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.
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.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');
You can specify additional options in the
compiler.build
command by using name-value arguments. For details, seecompiler.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 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 themwArray
API.MagicSquareComp.xml
— XML file that contains documentation for themwArray
assembly.MagicSquareComp_overview.html
— HTML file that contains requirements for accessing the assembly and for generating arguments using themwArray
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, 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.cs
that was generated in thesamples
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:
Uses a
try
-catch
block to handle exceptionsCreates an
MWNumericArray
array to store the input dataInstantiates the
MagicSquareClass
objectresults
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 argumentsWrites the function output to the console
In Visual Studio, add a reference to your assembly file
MagicSquareComp.dll
located in the folder where you generated or installed the assembly.Add a reference to the
MWArray
API.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
| Library Compiler | deploytool
| mcc