C#中調用Matlab程式

來源:互聯網
上載者:User

1,在matlab中建一個m檔案(test.m)

例如:
function test(h,D)
for x=-10:0.1:10,
   if x>D
       y=h;
       hold on;
       plot(x,y)
   elseif x<-D
       y=-h; 
       hold on;
       plot(x,y)
   else
       y=h/(D*x);
       hold on;
       plot(x,y)
   end
end

在MATLAB R2007a中輸入deploytool命令(在matlab6.5中輸入comtool),開啟Matlab Build For .net視窗建立個工程,在此工程中“Add files”即填加test.m這個檔案,build即可產生COM組件,為test.dll,即為我們將在C#中引用的COM組件。

Build時matlab已經自動將此dll在註冊表中註冊

不過之前我們還需做一個準備工作,開啟開始菜單“運行”,輸入regsvr32 "matlab安裝路徑\bin\win32\mwcomutil.dll",即對mwcomutil.dll進行註冊(這個dll是matlab下作的任何com組件都要用到的dll),下面我們在其他編譯器下調用時就可以用了。

2,下面即可在C#中調用dll檔案:

"引用"右擊,選擇瀏覽,找到test.dll,添加。
"引用"右擊,選擇瀏覽,找到MWArray.dll(matlab安裝路徑\toolbox\dotnetbuilder\bin\win32\v2.0\MWArray.dll),添加。
再寫入以下代碼:
            test.testclass st = new test.testclass();
            MathWorks.MATLAB.NET.Arrays.MWArray m = 1, n = 2;
            st.test(m,n);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.