Matlab .NET on Linux, macOS, and Windows
Matlab external language interfaces includes .NET on Windows, Linux, and macOS. This allows efficiently calling .NET assemblies and using .NET libraries directly from Matlab.
The Matlab function
dotnetenv
is used to set up and check the active .NET environment in Matlab.
Environment variable DOTNET_ROOT
is vital for Matlab to detect the .NET installation, particularly on Linux and macOS.
If Matlab is having issues detecting the .NET installation NET.isNETSupported
is false, determine the value for DOTNET_ROOT
from system Terminal:
dotnet --info
If “dotnet” command is not found, install .NET SDK:
- macOS:
brew install dotnet
- Windows:
winget install Microsoft.DotNet.SDK.9
# whatever the desired version is - Linux: from Microsoft
- Matlab Online (Linux): if .NET isn’t detected, use .NET install script to install .NET SDK.
Find the “dotnet” executable and run dotnet --info
to get the DOTNET_ROOT
path.
If this path is not defined, look for the Base Path, and pick the directory that contains the “dotnet” executable.
Set the DOTNET_ROOT
environment variable in Matlab by adding to Matlab startup.m so that future Matlab sessions have the correct DOTNET_ROOT
set.
edit(fullfile(userpath,'startup.m'))
Then add the following line to startup.m
:
setenv("DOTNET_ROOT", <DOTNET_ROOT path from dotnet --info>)
Restart Matlab. Do the following one-time command to finish setting up .NET in Matlab:
dotnetenv("core", Version=8)
The Version number must match the major version of .NET in DOTNET_ROOT. On future Matlab sessions, dotnetenv() will be empty until the first .NET command is run, for example
NET.isNETSupported
then (optionally) running dotnetenv()
shows the current .NET environment.