2015-04-29 81 views
-2

我有一些用Matlab編寫的代碼,但是我希望從C#控制檯應用程序調用此代碼。c#控制檯應用程序運行matlab函數

我不需要任何數據從Matlab返回到我的應用程序(雖然如果容易,會很高興看到)。

似乎有幾個選項,但不知道哪個最好。速度並不重要,因爲這將是一項自動化任務。

+2

因爲我,我只回答了這個最近研究了這個,並且從字面上打開了相關的網頁。這些信息不難發現或理解,您在未來提出問題之前必須做更好的研究。 –

回答

1

MATLAB有一個.Net接口,有很好的文檔。文章Call MATLAB Function from C# Client中涵蓋了您需要執行的操作。

對於一個簡單的MATLAB功能,說:

function [x,y] = myfunc(a,b,c) 
x = a + b; 
y = sprintf('Hello %s',c); 

..它歸結爲創建MLApp並調用Feval方法:

class Program 
{ 
    static void Main(string[] args) 
    { 
     // Create the MATLAB instance 
     MLApp.MLApp matlab = new MLApp.MLApp(); 

     // Change to the directory where the function is located 
     matlab.Execute(@"cd c:\temp\example"); 

     // Define the output 
     object result = null; 

     // Call the MATLAB function myfunc 
     matlab.Feval("myfunc", 2, out result, 3.14, 42.0, "world"); 

     // Display result 
     object[] res = result as object[]; 

     Console.WriteLine(res[0]); 
     Console.WriteLine(res[1]); 
     Console.ReadLine(); 
    } 
} 
相關問題