Test Your .NET Development Environment
MATLAB® engine API for .NET ships with example C# code to help test your environment and become familiar with using the engine API.
To open the folder containing the example code, run this command in the Command Window,
where matlabroot is the path returned by the MATLAB
matlabroot command:
fullfile(matlabroot,"extern","examples","engines","dotnet")Folder Contents
dotnet_engine_examples.sln— Microsoft® Visual Studio® project files.Program.cs— Console application that runs all examples in the project.MainWindow.xaml.cs— Windows® GUI application that calculates a factorial based on user input..csfiles — C# example source files.README— Text file that describes how to build and run the examples using Visual Studio.
Examples Overview
The examples show how to:
Call MATLAB functions from C#.
Handle exceptions.
Interact with MATLAB objects from a C# application.
Interact with MATLAB structs from a C# application.
Interact with the MATLAB base workspace.
Build and Run Examples
To build and run the examples, copy the example files to a writable folder on your path by running these commands in the Command Window:
copyfile(fullfile(matlabroot,"extern","examples","engines","dotnet","dotnet_engine_examples.sln"),".","f") copyfile(fullfile(matlabroot,"extern","examples","engines","dotnet","console\*.*"),".\console\","f")
If you are on a Windows platform, run this command to copy the Windows GUI application:
copyfile(fullfile(matlabroot,"extern","examples","engines","dotnet","gui\*.*"),".\gui\","f")
Follow these instructions in the README file.
Copyright 2023 The MathWorks, Inc.
These instructions are for compiling and running a C# application
that uses MATLAB Engine API for .NET from the command line.
Projects can also be opened in your favorite IDE.
In these instructions, replace <matlabroot> with the value returned
by the matlabroot function in MATLAB.
## PREREQUISITES ##
1. Install the latest .NET SDK from https://dotnet.microsoft.com/download
The full .NET SDK is required, not just the .NET Runtime.
2. Set up environment variables to point to your MATLAB installation.
Use these environment variable names and paths:
Windows:
PATH
<matlabroot>\extern\bin\win64
Apple Silicon:
DYLD_LIBRARY_PATH
<matlabroot>/extern/bin/maca64
Linux:
LD_LIBRARY_PATH
<matlabroot>/extern/bin/glnxa64:<matlabroot>/sys/os/glnxa64
3. Copy the example files to a working directory for example "C:\work".
The files that need to be copied are as follows:
Windows:
dotnet_engine_examples.sln
all files in the console and gui directories
Apple macOS and Linux:
dotnet_engine_examples.sln
all files in the console directory
## COMPILING ##
Use the 'dotnet build' command to compile all example projects.
$> cd C:\work
$> dotnet build /p:matlabroot=<matlabroot>
## RUNNING ##
Use the 'dotnet run' command to execute a particular example project.
To run the console example:
$> dotnet run --no-build --project console
To run the gui example (Windows only):
$> dotnet run --no-build --project gui
