Requirements to Build .NET Engine Programs
To set up your .NET environment for building engine applications:
Make sure you have a supported version of .NET.
Set environment variables.
Build and run your .NET code.
Supported Versions of .NET
Build the engine application with a supported version of .NET. For version information, see MATLAB Interfaces to Other Languages. Install both the .NET SDK and the .NET Runtime from https://dotnet.microsoft.com/download.
Run-Time Environment
To run your application, set one of these environment variables to the specified path.
Operating System | Variable | Path |
---|---|---|
Windows® |
|
|
macOS with Apple silicon |
|
|
macOS with Intel® |
|
|
Linux® |
|
|
Build and Run .NET Projects from CLI
Use the .NET command-line interface (CLI) along with a code editor to create .NET applications. For more information, see .NET CLI Overview in the Microsoft® documentation. To work with C# examples shipped with MATLAB®, see Test Your .NET Development Environment.
Open the operating system command prompt and navigate to a writeable folder.
At the command line, set the run-time environment variable.
Create the project
MyApp
.dotnet new console --name MyApp
This command creates a folder named
MyApp
that contains:obj
folderMyApp.csproj
project fileProgram.cs
C# source file
Open the project file in a text editor and add these references to the project using the
<ItemGroup>
tag. The files are in a folder defined byfullfile(
.matlabroot
,"extern","dotnet","netstandard2.0")MathWorks.MATLAB.Engine
MathWorks.MATLAB.Types
Enable use of the
dynamic
keyword by adding a reference toMicrosoft.CSharp
using the<PackageReference>
tag.Verify that the target framework is a supported version using the
<TargetFramework>
tag. For version information, see MATLAB Interfaces to Other Languages.Your project file should resemble the following:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> </PropertyGroup> <ItemGroup> <!-- Enables interop between .NET and MATLAB --> <Reference Include="MathWorks.MATLAB.Types" > <HintPath>$(matlabroot)/extern/dotnet/netstandard2.0/MathWorks.MATLAB.Types.dll</HintPath> </Reference> <!-- Provides an interface to MATLAB Engine API --> <Reference Include="MathWorks.MATLAB.Engine" > <HintPath>$(matlabroot)/extern/dotnet/netstandard2.0/MathWorks.MATLAB.Engine.dll</HintPath> </Reference> <!-- Enables using the 'dynamic' keyword --> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> </ItemGroup> </Project>
Open the C# source file
Program.cs
and replace the existing code with the following code:At the command line, build your C# project, specifying
matlabroot
. For example, ifmatlabroot
isC:\Program Files\MATLAB\R2022b
, then type:cd MyApp dotnet build /p:matlabroot="C:\Program Files\MATLAB\R2022b" MyApp.csproj
At the command line, run your application:
dotnet run --no-build
The application displays a magic square.
Build and Run .NET Projects from Microsoft Visual Studio
As an alternative to the interactive command line approach to creating a .NET application, you can create the application using Microsoft Visual Studio®.
In Visual Studio, create a .NET 5.0 C# project named
MyApp
. For details, see the Create the app section in Create a .NET console application using Visual Studio in the Microsoft documentation.In the Solution Explorer in Visual Studio, right-click the project name and select Add > Project Reference. In the Reference Manager window, click Browse and add these references. The files are in a folder defined by
fullfile(
.matlabroot
,"extern","dotnet","netstandard2.0")MathWorks.MATLAB.Engine
MathWorks.MATLAB.Types
Open the C# source file
Program.cs
and replace the existing code with the code provided in the Open the C# source file step of the previous Build and Run .NET Projects from CLI section.Build and run the application.