Requirements to Build .NET Engine Programs - MATLAB & Simulink (original) (raw)
Main Content
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® | PATH | _matlabroot_\extern\bin\win64 |
macOS with Apple silicon | DYLD_LIBRARY_PATH | matlabroot/extern/bin/maca64 |
macOS with Intel® | DYLD_LIBRARY_PATH | matlabroot/extern/bin/maci64 |
Linux® | LD_LIBRARY_PATH | matlabroot/extern/bin/glnxa64:matlabroot/sys/os/glnxa64 |
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 writable 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: Exe net6.0 $(matlabroot)/extern/dotnet/netstandard2.0/MathWorks.MATLAB.Types.dll $(matlabroot)/extern/dotnet/netstandard2.0/MathWorks.MATLAB.Engine.dll
// The main entry point for the application.
static void Main() {
using (dynamic matlab = MATLABEngine.StartMATLAB()) {
double xIn = 5.0;
double [,] results = matlab.magic(xIn);
RunOptions opts = new RunOptions() { Nargout = 0 };
matlab.disp(opts, "Hello, MATLAB!");
for (int i = 0; i < results.GetLength(0); i++) {
for (int j = 0; j < results.GetLength(1); j++) {
Console.Write("{0} ", results[i, j]);
}
Console.WriteLine();
}
}
}
}
}
9. At the command line, build your C# project, specifying_matlabroot
. For example, ifmatlabroot
_ is C:\Program Files\MATLAB\R2022b
, then type:
cd MyApp
dotnet build /p:matlabroot="C:\Program Files\MATLAB\R2022b" MyApp.csproj
10. At the command line, run your application:
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 > . 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.