Requirements to Build .NET Engine Programs - MATLAB & Simulink (original) (raw)

Main Content

To set up your .NET environment for building engine applications:

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.

  1. Open the operating system command prompt and navigate to a writable 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:

  1. 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
  2. Enable use of the dynamic keyword by adding a reference toMicrosoft.CSharp using the <PackageReference> tag.
  3. Verify that the target framework is a supported version using the<TargetFramework> tag. For version information, see MATLAB Interfaces to Other Languages.
  4. 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
8. Open the C# source file `Program.cs` and replace the existing code with the following code: [![](https://in.mathworks.com/help/includes/product/images/doc_center/arrow_right.gif)](#) [Program.cs](#) // ******************************************************************************* // // Program.cs // // This example demonstrates how to use MATLAB .NET Assembly to build a simple // component returning a magic square. // // Copyright 2022 The MathWorks, Inc. // // ******************************************************************************* using System; using MathWorks.MATLAB.Engine; using MathWorks.MATLAB.Exceptions; using MathWorks.MATLAB.Types; namespace MathWorks.MATLAB.Engine.ConsoleExamples { public class Program {
    // 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®.

  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 > . In the Reference Manager window, click Browse and add these references. The files are in a folder defined byfullfile(_`matlabroot`_,"extern","dotnet","netstandard2.0").
    • MathWorks.MATLAB.Engine
    • MathWorks.MATLAB.Types
  3. Open the C# source fileProgram.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.

See Also

Topics