Main Content

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 SystemVariablePath

Windows®

PATH

matlabroot\extern\bin\win64

macOS

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.

  1. Open the operating system command prompt and navigate to a writeable folder.

  2. At the command line, set the run-time environment variable.

  3. Create the project MyApp.

    dotnet new console --name MyApp

    This command creates a folder named MyApp that contains:

    • obj folder

    • MyApp.csproj project file

    • Program.cs C# source file

  4. 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 by fullfile(matlabroot,"extern","dotnet","netstandard2.0").

    • MathWorks.MATLAB.Engine

    • MathWorks.MATLAB.Types

  5. Enable use of the dynamic keyword by adding a reference to Microsoft.CSharp using the <PackageReference> tag.

  6. Verify that the target framework is a supported version, such as net5.0 or net6.0, using the <TargetFramework> tag. For version information, see MATLAB Interfaces to Other Languages.

  7. Your project file should resemble the following:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.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>
  8. Open the C# source file Program.cs and replace the existing code with the following code:

     Program.cs

  9. At the command line, build your C# project, specifying matlabroot. For example, if matlabroot 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:

    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®.

  1. 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.

  2. 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

  3. 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.

  4. Build and run the application.

Related Topics