メインコンテンツ

.NET 開発環境のテスト

.NET 用の MATLAB® エンジン API には、C# コード例が付随しています。このコード例は、環境をテストし、エンジン API の使用を習得するのに役立ちます。

コード例を含むフォルダーを開くには、コマンド ウィンドウで次のコマンドを実行します。ここで、matlabroot は MATLAB matlabroot コマンドによって返されるパスです。

fullfile(matlabroot,"extern","examples","engines","dotnet")

フォルダーの内容

  • dotnet_engine_examples.sln — Microsoft® Visual Studio® プロジェクト ファイル。

  • Program.cs — プロジェクト内のすべての例を実行するコンソール アプリケーション。

    MainWindow.xaml.cs — ユーザー入力に基づいて階乗を計算する Windows® GUI アプリケーション。

  • .cs ファイル — C# の例のソース ファイル。

  • README — Visual Studio を使用した例のビルドと実行方法について説明するテキスト ファイル。

例の概要

これらの例では、以下の方法が示されます。

  • C# から MATLAB 関数を呼び出す。

     BasicExample.cs

  • 例外を処理する。

     ExceptionExample.cs

  • C# アプリケーションから MATLAB オブジェクトを操作する。

     ObjectExample.cs

  • C# アプリケーションから MATLAB 構造体を操作する。

     StructExample.cs

  • MATLAB ベース ワークスペースを操作する。

     WorkspaceExample.cs

例のビルドと実行

例をビルドして実行するには、コマンド ウィンドウで次のコマンドを実行して、例のファイルをパス上の書き込み可能なフォルダーにコピーします。

copyfile(fullfile(matlabroot,"extern","examples","engines","dotnet","dotnet_engine_examples.sln"),".","f")
copyfile(fullfile(matlabroot,"extern","examples","engines","dotnet","console\*.*"),".\console\","f")

Windows プラットフォームでは、次のコマンドを実行して Windows GUI アプリケーションをコピーします。

copyfile(fullfile(matlabroot,"extern","examples","engines","dotnet","gui\*.*"),".\gui\","f")

README ファイルに記載されている次の指示に従います。

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

参考

トピック