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.
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.
|MWArray API Reference|
Create Function in MATLAB
In MATLAB, examine the MATLAB code that you want to package. For this example, open
function y = makesquare(x) y = magic(x); disp(y)
At the MATLAB command prompt, enter
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.
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
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.
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
makesquareas the name of the assembly. Rename it as
MagicSquareComp. The same name is followed through in the implementation of the assembly.
Verify that the function defined in
makesquare.mis 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);
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.
Additional runtime settings — Platform-specific options for controlling the generated executable. 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.
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 more information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.
The log file
PackagingLog.htmlcontains packaging results.
Create .NET Assembly Using
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.
Save the path to the file
. For example, if you are using Visual Studio version 15, type:
appFile = fullfile(matlabroot,'toolbox','dotnetbuilder','Examples', ... 'VS15','NET','MagicSquareExample','MagicSquareComp','makesquare.m');
Save the following code in a sample file named
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(appFile, ... 'AssemblyName','MagicSquareComp', ... 'ClassName','MagicSquareClass', ... 'SampleGenerationFiles','makesquareSample1.m');
buildResultscontains information on the build type, generated files, and build options.
The function generates the following files within a folder named
MagicSquareCompdotNETAssemblyin 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
MagicSquareComp.xml— XML file that contains documentation for the
MagicSquareComp_overview.html— HTML file that contains requirements for accessing the assembly and for generating arguments using the
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.
The generated assembly does not include MATLAB Runtime or an installer. To create an installer using the
You can specify additional options in the
compiler.buildcommand 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.
Open Microsoft Visual Studio and create a C# Console App (.NET Framework) called
Remove any source code files that were created within your project, if necessary.
Add the generated sample .NET application code
for_redistribution_files_only\samplesfolder to the project.
The program listing is shown below.
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
If MATLAB is installed on your system
If MATLAB Runtime is installed on your system
Go 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
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
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
Visual Basic file