close

使用:

  1. matlab 2015a
  2. Visual Studio Community 2015

 

要把matlab function給C#呼叫,最簡單的方是就是弄成dll檔了!

首先,我們寫個matlab function

 

之後在Command Windows輸入deploytool

 

會出現Compiler視窗,由於我們是要做成library給外部呼叫,所以是選擇Library Compiler

 

接著如下圖所示操作

都完成後按下右上的Package即可產生DLL檔(可從右上的Setting設定輸出的資料夾)

 

正在生成檔案...

 

生成完畢如下,其中紅框處是DLL檔的位置

 

接著開啟Visual Studio,建立一個C#專案,並命名為Practice

對參考點選右鍵→加入參考

 

按下瀏覽,加入 for_redistribution_files_only 資料夾下的 MatlabDLL.dll

以及 MWArray.dll

該DLL的路徑位於 Matlab\R2015a\toolbox\dotnetbuilder\bin\win64\v2.0

 

接著開始撰寫C#程式碼


using MathWorks.MATLAB.NET.Arrays;
using MatlabDLL;
using System;

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
            POW p = new POW();
            MWArray mw = p.Pow((MWArray)2, (MWArray)10);//計算2的10次方
            int res = int.Parse(mw.ToString());

            Console.WriteLine(res);
            Console.ReadKey();
        }
    }
}


 

執行結果如下

arrow
arrow
    全站熱搜

    Yang 發表在 痞客邦 留言(0) 人氣()