Supported Platform: Windows® (Authoring), Linux® (Execution), and macOS (Execution).
This example shows how to integrate and run a .NET assembly created using the Library Compiler with a .NET Core application.
Goal: Return an n-by-n magic square.
Create a new work folder that is visible to the MATLAB® search path. This example uses C:\Work
as the new
work folder.
Install MATLAB Runtime on Windows and on additional platforms where you plan on running your .NET Core
application. For Linux and macOS platforms, after installing the runtime, you need to set the
LD_LIBRARY_PATH
and DYLD_LIBRARY_PATH
environment variables respectively. For more information, see MATLAB Runtime Path Settings for Run-Time Deployment.
Verify that you have Visual Studio® and .NET Core 2.0 or higher installed. If you have version 15.8.2 of Visual Studio 2017 installed, then you do not need to install .NET Core 2.0 or higher separately.
Create a new MATLAB file named mymagic.m
with the following code in the
work folder:
function out = mymagic(in)
out = magic(in);
Type libraryCompiler
at the MATLAB command line to launch the Library Compiler app.
In the TYPE section of the toolstrip, select
.NET Assembly
, and in the EXPORTED
FUNCTIONS section click the Add button the to add
the file mymagic.m
to the project.
In the Library information section, name the library
MyMatrixFunctions
. Change the default class name from
Class1
to MyMagic
.
Save the deployment project with the default project name
MyMatrixFunctions
.
Select Package to create a .NET assembly. For information about the created files, see Files Generated After Packaging MATLAB Functions.
Open the Command shell in Windows and navigate to the folder C:\Work
.
At the command line, type:
dotnet new console --name MyDotNetCoreApp
This creates a folder named MyDotNetCoreApp
that has the
following contents:
obj
folder
MyDotNetCoreApp.csproj
project file
Program.cs
C# source file
Open the project file in a text editor.
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> </Project> |
Add the following references to the project using the
<ItemGroup>
tag:
.NET assembly file MyMatrixFunctions.dll
created by the
Library Compiler app
MWArray.dll
from the MATLAB Runtime
After you add the references your project file looks like this:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.2</TargetFramework> </PropertyGroup> <ItemGroup> <Reference Include="MyMatrixFunctions"> <HintPath>C:\work\MyMatrixFunctions\for_redistribution_files_only\MyMatrixFunctions.dll</HintPath> <!--Path to .NET Assembly created by Library Compiler app--> </Reference> <Reference Include="MWArray"> <HintPath>C:\Program Files\MATLAB\MATLAB Runtime\v97\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll</HintPath> <!--Path to MWArray.dll in the MATLAB Runtime--> </Reference> </ItemGroup> </Project> |
Open the C# source file Program.cs
and replace the existing
code with the following code:
At the Command shell, build your .NET Core project by typing:
dotnet build MyDotNetCoreApp.csproj
At the Command shell, run your application by typing the following:
dotnet run -- 3
This displays a 3x3 magic square.
Publish the project as a self-contained deployment to execute the application on either Linux or macOS. This example publishes to Linux. At the Command shell, type:
dotnet publish --configuration Release --framework netcoreapp2.2 --runtime linux-x64 --self-contained true MyDotNetCoreApp.csproj
To publish to macOS, type:
dotnet publish --configuration Release --framework netcoreapp2.2 --runtime osx.10.11-x64 --self-contained true MyDotNetCoreApp.csproj
Copy the Release
folder from
C:\Work\MyDotNetCoreApp\bin
on Windows to ~/Work
on a Linux machine.
On the Linux machine, verify that you have installed MATLAB Runtime and set up your LD_LIBRARY_PATH
environment variable.
For more information, see Prerequisites.
Open a Bash shell and navigate to:
~/Work/Release/netcoreapp2.2/linux-x64/publish
Run the .NET Core application by typing:
./MyDotNetCoreApp 3
Output
Magic square of order 3 8 1 6 3 5 7 4 9 2 Magic square as native array: Element(0,0)= 8 Element(0,1)= 1 Element(0,2)= 6 Element(1,0)= 3 Element(1,1)= 5 Element(1,2)= 7 Element(2,0)= 4 Element(2,1)= 9 Element(2,2)= 2