Supported platform: Windows®
This example shows how to create a .NET Assembly for a MATLAB® function. You can integrate the generated assembly into a .NET application. This example also shows how to call the .NET assembly from a .NET application. The target system does not require a licensed copy of MATLAB to run the application.
In MATLAB, examine the MATLAB code that you want packaged. For this example, open makesquare.m
located in
.matlabroot
\toolbox\dotnetbuilder\Examples\VSVersion
\NET\MagicSquareExample\MagicSquareComp
At the MATLAB command prompt, enter makesquare(5)
.
The output is:
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
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 by entering:
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, navigate to
and select matlabroot
\toolbox\dotnetbuilder\Examples\VSVersion
\NET\MagicSquareExample\MagicSquareCompmakesquare.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 filename of the installer.
Runtime included in package — Generate an application that includes the MATLAB Runtime installer. You can specify the filename of the installer.
Note
The first time you select this option, you are prompted to download the MATLAB Runtime installer.
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.
Verify that the function defined in makesquare.m
is mapped
into Class1
.
Add MATLAB files to generate the sample .NET driver files. Although .NET driver files are not necessary to create an assembly, they are used to demonstrate how to Build a .NET Application.
In the Samples section, select Create New
Sample, and click makesquare.m
. A MATLAB file opens for you to edit. Define the input variables as
necessary for your application, save the file, and return to the
Library Compiler app. For more information and
limitations, see Sample Driver File Creation.
You can customize the installer, customize your application, and add more information about the application as follows:
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.
To 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.
Three folders are generated in the target folder location:
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.
PackagingLog.txt
— Log file generated by
MATLAB
Compiler™.
compiler.build.dotNETAssembly
Note
If you have already created a .NET assembly using the Library Compiler app, you can skip this section. However, if you want to know how to create a .NET assembly from the MATLAB command window using a programmatic approach, follow these instructions.
Save the path to the makesquare.m
file located in
.
For example, if using Visual Studio® version 15, type:matlabroot
\toolbox\dotnetbuilder\Examples\VSVersion
\NET\MagicSquareExample\MagicSquareComp
appFile = 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 add a sample file and specify the assembly name.
buildResults = compiler.build.dotNETAssembly(appFile,... 'AssemblyName','MagicSquareComp',... 'SampleGenerationFiles','makesquareSample1.m');
The compiler.build.Results
object
buildResults
contains information on the build type,
generated files, and build options.
This syntax 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
information on integrating your assembly.
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 component 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.
Additional options can be specified 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.
After creating your .NET assembly file, you can call it from a .NET application. The .NET application that you create uses the sample .NET driver code generated during packaging. The .NET driver code calls the .NET assembly file, and it is based on the sample MATLAB file you selected in previous setup steps.
These steps are also explained in the GettingStarted.html
file in
for_redistribution_files_only
folder. Before starting, make sure
that you Install and Configure MATLAB Runtime, and that you have Microsoft®
Visual Studio installed.
Install the .NET assembly from the for_redistribution
folder, or distribute the assembly file MagicSquareComp.dll
to the target machine.
The generated shared libraries and support files are located in the
for_testing
folder.
Open Microsoft
Visual Studio and create a project. For this example, create a C# Console
Application called MainApp, and create a reference to your
assembly file MagicSquareComp.dll
.
Ensure that the assembly is located in the application folder created where you installed the component.
Create a reference to the MWArray
API. The location of the
API within MATLAB Runtime
R2021a is:
matlabroot
\MATLAB Runtime\v910\toolbox\dotnetbuilder\bin\<arch>
\<version>
\MWArray.dll
Go to Build > Configuration Manager, and change the platform from Any CPU to x64.
Copy the generated sample .NET driver code
makesquareSample1.cs
from the
for_redistribution_files_only\samples
folder into the
project, and save it.
After you finish writing your code, build and run it with Microsoft Visual Studio.
compiler.build.dotNETAssembly
| deploytool
| libraryCompiler
| mcc