C# MATLAB混合編程

來源:互聯網
上載者:User

標籤:http   io   os   ar   for   sp   檔案   on   art   

我附帶把matlab配置過程也給大家上傳上來。
【轉載】終於學會C#調用matlab函數了,原來這麼簡單(也可以下載附件查看)
自己的配置:

(1)Microsoft Visual Studio 2005

(2)Matlab R2009a

(3)iis 6.0

第一步:

首先安裝Matlab;

安裝Visual Studio 2005或者更高版本;

安裝MCRInstall.exe,我安裝完Matlab之後在這裡找得的:D:"Program Files\MATLAB\R2009b\toolbox\compiler\deploy\win32

點擊:我的電腦-屬性-進階-環境變數-系統變數-PATH-編輯,在變數值輸入框中,不要刪除以前的字串,在最前面加入MCR的安裝路徑,如:D:"Program Files\MATLAB\MATLAB Compiler Runtime\v80\bin\win32; 然後確定、儲存、重啟電腦。(我就是不知道需要這兩步,老是在C#建立Matlab的訪問對象時,出現:“xxx的類型初始值設定項引發異常。”,完全不理解是什麼原因,被耽擱了大量時間,直到安裝了Matlab 2008b,仔細閱讀它產生的readme.txt才知道)

第二步: 在matlab的Command window中輸入mbuild -setup顯示如下

>> mbuild -setup

Please choose your compiler for building standalone MATLAB applications: Would you like mbuild to locate installed compilers [y]/n? n %選擇n

Select a compiler:

[1] Lcc-win32 C 2.4.1

[2] Microsoft Visual C++ 6.0

[3] Microsoft Visual C++ .NET 2003

[4] Microsoft Visual C++ 2005

[5] Microsoft Visual C++ 2005 Express Edition

[6] Microsoft Visual C++ 2008

[0] None Compiler: 4 %選擇4,其他編譯器可以選相應的選項,我沒有驗證過

The default location for Microsoft Visual C++ 2008 compilers is C:\Program Files\Microsoft Visual Studio 8, but that directory does not exist on this machine.

Use C:\Program Files\Microsoft Visual Studio 8.0 anyway [y]/n? n%選擇n

Please enter the location of your compiler: [C:\Program Files\Microsoft Visual Studio 9] D:\Program Files\Microsoft Visual Studio 8.0 %選擇您自己的當前安裝路徑

Please verify your choices: Compiler: Microsoft Visual C++ 2005 Location: D:\Program Files\Microsoft Visual Studio 8.0 Are these correct [y]/n? y %看上述資訊,如果正確選擇y

Warning: MBUILD requires that the Microsoft Visual C++ 8.0 directories "VC" and "Common7" be located within the same parent directory. MBUILD setup expected to find directories named "Common7" and "VC" in the directory: "C:\Program Files\Microsoft Visual Studio 8". Trying to update options file: C:\Documents and Settings\Administrator\Application Data\MathWorks\MATLAB\R2009a\compopts.bat From template: D:\PROGRA~1\MATLAB\R2009a\bin\win32\mbuildopts\msvc80compp.bat Done . . . 到此matlab編譯器設定成功。

第三步: 編寫m檔案:

function y=mymagic(x)

y=magic(x);

第四步:建立項目:

在matlab中點擊“File- new -Development Project” 自己選擇項目儲存目錄和項目名,如E:"和magicpro.prj 類 型選擇.NET Component,如果你要產生更通用的COM組件,選擇Generic COM Component。添加剛才的m檔案到這個建立的項目中去。點擊Build the project按鈕(這個按鈕的表徵圖和微軟開發工具的Build表徵圖一樣)或者右擊選擇“build”,等待3,4分鐘。建立成功。

mcc -W ‘dotnet:magicpro,Magicpro,0.0,private‘ -d ‘E:\magicpro\src‘ -T ‘link:lib‘ -v ‘class{Magicpro:\work\mymagic.m}‘ Compiler version: 4.10 (R2009a) Processing include files... 2 item(s) added. Processing directories installed with MCR... The file E:\magicpro\src\mccExcludedFiles.log contains a list of functions excluded from the CTF archive. 2 item(s) added. Generating MATLAB path for the compiled application... Created 41 path items. Begin validation of MEX files: Sat Mar 27 09:22:22 2010 End validation of MEX files: Sat Mar 27 09:22:22 2010 Parsing file "D:\work\mymagic.m" (Referenced from: "Compiler Command Line". Parsing file "D:\Program Files\MATLAB\R2009a\toolbox\compiler\deploy\deployprint.m" (Referenced from: "Compiler Command Line". Parsing file "D:\Program Files\MATLAB\R2009a\toolbox\compiler\deploy\printdlg.m" (Referenced from: "Compiler Command Line". Deleting 0 temporary MEX authorization files. Generating file "E:\magicpro\src\Magicpro.cs". Generating file "E:\magicpro\src\MagicproNative.cs". Generating file "E:\magicpro\src\readme.txt". Generating file "E:\magicpro\src\dotnet_mcc_component_data.cs". Executing command: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe /nologo /optimize /doc:"E:\magicpro\src\magicpro.xml" /t:library /r:"D:\Program Files\MATLAB\R2009a\toolbox\dotnetbuilder\bin\win32\v2.0\MWArray.dll" /resource:"E:\magicpro\src\magicpro.ctf" /out:"E:\magicpro\src\magicpro.dll" "E:\magicpro\src\Magicpro.cs" "E:\magicpro\src\dotnet_mcc_component_data.cs" Executing command: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe /nologo /optimize /doc:"E:\magicpro\src\magicproNative.xml" /t:library /r:"D:\Program Files\MATLAB\R2009a\toolbox\dotnetbuilder\bin\win32\v2.0\MWArray.dll" /resource:"E:\magicpro\src\magicpro.ctf" /out:"E:\magicpro\src\magicproNative.dll" "E:\magicpro\src\MagicproNative.cs" "E:\magicpro\src\dotnet_mcc_component_data.cs" Copying: E:\magicpro\src\magicpro.dll -> E:\magicpro\distrib Copying: E:\magicpro\src\magicproNative.dll -> E:\magicpro\distrib Copying: E:\magicpro\src\readme.txt -> E:\magicpro\distrib Compilation completed successfully. The output is located in E:\magicpro\distrib. You can package the component by clicking on the "Package" icon in the Deployment Tool toolbar, or by clicking the Tools->Package menu when the Deployment Tool panel is selected. To include additional files in the package, click Project->Settings).

第五步: 開啟2005.建立網站 建立C#網頁添加引用:

選擇.NET,選擇MathWorks,.NET MWArrays API字樣的一個組件添加引用:選擇瀏覽,選擇剛才用Deployment建立的項目的E:"magicpro\distrib目錄的magicpro.dll檔案 . 設定頁面,一個文字框(輸入魔方的維數)一個按鈕(提交),一個標籤(顯示魔方)。 代碼如下:

using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MathWorks.MATLAB.NET.Arrays;
using magicpro;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Visible = false;
    }
  

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Visible = true;
        string html_value = "<table >";
        int input = Convert.ToInt32(TextBox1.Text);
        magicpro.Magicpro m = new Magicpro();
        MWNumericArray x = new MWNumericArray(input);
        MWArray[] argsout = new MWArray[1];
        MWArray[] argsin = new MWArray[] { x };
        m.mymagic(1, ref argsout, argsin);
        MWNumericArray y = argsout[0] as MWNumericArray;
      
        TextBox1.Visible = false;
        Button1.Visible = false;
        for (int i = 1; i <= y.Dimensions[0]; i++)
        {
            html_value += "<tr>";
            for (int j = 1; j <= y.Dimensions[1]; j++)
            {
                html_value += "<td>";
                html_value += y[i, j];
                html_value+="</td>";
            }
            html_value += "</tr>";
        }
        html_value += "</table>";
        Label1.Text = html_value;
    }
}


運行輸入4.點擊按鈕出現4階魔方:

16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1 成功

C# MATLAB混合編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.