初步探索如何使用Visual C#調用Matlab

來源:互聯網
上載者:User

Matlab 2007a,
2007b, 2008a,
2008b等都對開發獨立於Matlab運作平台的Matlab應用程式有很好的支援。網址mathworks.com/products/compiler/demos.html
下提供了一些Matlab程式展開應用的執行個體,其中把純的Matlab程式轉換成獨立執行的應用程式顯得很簡單。比如連結mathworks.com/products/demos/compiler/deploytool/index.html提供了如何把.m檔案轉換成獨立啟動並執行.exe可執行檔。由於用Visual
C#調用Matlab的資料不多,經過初步探索,本文對這個問題用一個執行個體做個說明。

需要的工具:Matlab,Matlab Builder NE
Toolbox,Matlab Compiler Toolbox, Visual
C#

首先編寫一個簡單的Matlab函數,如下,並存為sumab.m(預設時存到Matlab的預設工作目錄下)
function
[tsum] = sumab(a, b)
tsum =
sum([a,b]);

下面說明如何用VC#來調用這個函數。
在Matlab的Command Window下運行"mbuild
–setup" 和 "deploytool",詳細步驟參見blog.sina.com.cn/s/blog_4b94ff130100d4uf.html。
一般情況下,運行"mbuild
–setup"時,讓系統自動搜尋編譯器就可以了。deploytool運行後,取一個工程名test_combination_matlab_c,選擇.NET
Component,會產生一個新工程。把sumab.m
加入到新工程中,然後Build,工程目錄下的Distrib目錄裡就會有test_combination_matlab_c.dll,可以供VC#使用。也可以Build
Matlab裡面內建的庫函數,就如例子blog.sina.com.cn/s/blog_4b94ff130100d4uf.html
所做的。
開啟Visual Studio, 建立一個VC#工程,筆者建的是Test項目。建好後,在Solution
Explorer裡,滑鼠右鍵單擊References->Add References->Browse,
加入產生的test_combination_matlab_c/Distrib/test_combination_matlab_c.dll;重複操作,加入Matlab目錄下的另一個dll檔案,/toolbox/dotnetbuilder/bin/win32/v2.0/MWArray.dll。

上面把該有的環境都設定好了,剩下的就是編程了。開啟VC#工程裡的主檔案UnitTest1.cs,如果用其他類型的項目,相應開啟項目的主檔案。檔案的開始部分加入
using
MathWorks.MATLAB.NET.Arrays;
如要畫圖,加
using
MathWorks.MATLAB.NET.Utility;
在主檔案的主程式 public void TestMethod1()
裡,加
MWArray a = 1, b = 2,
c;
test_combination_matlab_c.Test_combination_matlab_c sumob = new
test_combination_matlab_c.Test_combination_matlab_c();
c = sumob.sumab(a,
b);
在Debug時,加入BreakPoint,配合Quick
Watch,就可以看到c值為3.

這樣,一個簡單的調用程式就編好了。如果是數組矩陣,可以如下編寫:
MWNumericArray aa =
new double[2, 2] { { 1, 2 }, { 3, 4 } };
MWNumericArray bb = new double[2, 2]
{ { 1, 2 }, { 3, 4 } };
MWArray cc;
cc = sumob.sumab((MWArray)aa,
(MWArray)bb);
注意資料類型的轉換double<->MWNumericArray<->MWArray,參照網址
hi.baidu.com/adda/blog/item/c19bd33f3d87a6c77d1e714f.html

如果輸入數組矩陣複雜,可以先在Matlab定義,再輸入到VC#中。一個簡單的輸入如下所示,並存之為definematrixes.m
function
[a, b] = definematrixes()
a = [1 2;3 4];
b = [3 4;5
6];
把definematrixes.m加入deploytool當前工程(test_combination_matlab_c.),重新build之後,轉到VC#的項目,在主程式中加入下面代碼:
MWArray
aaa, bbb, ccc;
MWArray[] tempmatrix;
tempmatrix =
sumob.definematrixes(2);
aaa = tempmatrix[0];
bbb = tempmatrix[1];
ccc
= sumob.sumab(aaa,
bbb);
這樣,在Debug時,可以看到ccc={4,6,8,10}。這裡有個小問題沒解決,aaa.Dimension和bbb.Dimension都是2*2,不知為何,ccc就成了1*4了。如果沒有好的方法,可以用Matlab的reshape函數把1*4維變回2*2維的矩陣—只是要把reshape也加入到deploytool
的工程test_combination_matlab_c中。

對單一資料(非數列和矩陣),筆者用form的方法實現了使用者的輸入輸出。首先,建立一個基於Windows
Form的項目,在Form裡面,加入三個Label,三個Textbox組件以及一個Button組件(組件的大小位置剛開始可以隨意些),再把下面的程式拷貝進Form1.cs即可—同樣的要在References裡頭加入MWArray.dll和test_combination_matlab_c.dll。
using
System;
using System.Collections.Generic;
using
System.ComponentModel;
using System.Data;
using System.Drawing;
using
System.Linq;
using System.Text;
using System.Windows.Forms;
using
MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
namespace
Matlab_csharp_Forms_Application
{
    public partial class Form1 :
Form
    {
        public Form1()
        {
          
 InitializeComponent();
        }
        private void Form1_Load(object
sender, EventArgs e)
        {
     this.label1.Visible = false;
    
       this.textBox1.Visible = false;
            this.label2.Visible =
false;
            this.label3.Visible = false;
        }
}
    
   private void button1_Click(object sender, EventArgs e)
        {
    
       MWArray a = 1, b = 2, c;
          
 test_combination_matlab_c.Test_combination_matlab_c sumob = new
test_combination_matlab_c.Test_combination_matlab_c();
            try
 
          {
                a =
System.Convert.ToDouble(this.textBox2.Text);
                b =
System.Convert.ToDouble(this.textBox3.Text);
                c =
sumob.sumab(a, b);
                this.label1.Visible = true;
          
     this.textBox1.Visible = true;
                this.textBox1.ReadOnly =
true;
                this.textBox1.Text = c.ToString();
               
this.label2.Visible = false;
                this.label3.Visible =
false;
            }
            catch (System.FormatException)      
 
          {
                this.label1.Visible = false;
               
this.textBox1.Visible = false;
                this.label2.Visible =
true;
                this.label3.Visible = true;
                //
Statements for handling the exception
            }
    
   }
}

由於不清楚Visual
C#如何顯示數列和矩陣,大的資料可能輸出到Excel或SQL會是不錯的選擇,至於輸入,也許可以考慮在Matlab中寫輸入資料的函數(如上所述);熟悉VC#者或許可以給出適合的答案。

聯繫我們

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