C#中调用 matlab dll 作曲线拟和 
2011 年 06 月 06 日 今天是端午节,闲来无事,总结一下前一段时间应用
matlab 的成果。 
 
一、  使用环境: Visual Studio 2005,Matlab R2010b。 
 
二、操作步骤: 
1、 matlab 中安装 MCR 
2、 安装 matlab 编译器 
点击 MATLAB7\toolbox\compiler\deploy\win32 目录下 MCRInstaller。
安  提示安装。 
在 matlab 命令行输入 mbuild -setup,设置编译器,在设置过程中会出现选
择编译器的提示,我的电脑安装的是 Microsoft Visual C++ 2005 SP1,因
此选 Microsoft Visual C++ 2005 SP1.见图 
 
3、 运行 deploytool,编译.net dll 
在 matlab 命令行输入,出现如下窗口 
 
 
在 Name 中输入你将生成的 DLL 文件各,Location 中输入存入文件路径,在
Target 中点击下拉框,要发现我们可以将 matlab 文件处理成那些文件 
Windows standalone application 处理 windows 成独立可运行文件 
Console Application 控制台可执行文件 
C Shared Library  C 语言可分享库 
C++ Shared Library  C++语言可分享库 
Excel Add-in Excel 中可调用的库 
.NET Assembly  .NET 编译库 
Generic COM component 通用 COM 控件 
Java Package Java 包 
 
在此我们选择 .NET Assembly,确认后出现以下窗口 
 
编译前先要设置一下,在上图中点击 Settings 
 
设置.NET,设置如上图,选择 microsoft framwork 版本时一定要选单前使用版
本,不能选 default,要不然编译出来的会有一点问题。 
 
将 Assembly Type 设置为 Shared 
点击 Close 加到编译窗口 
 
   Add Class 中增加 invgamma 
   Add files test.m 文件  
   Test.m 文件内容如下 
  
 
 
 
 
 编译文件  
点击
 
编译结束后会在 test\src 下会产生 test.dll,这就是我们需求的 dll 文件  
 
 
4、 打开 vs2005 建立 c#项目 vstest 
5、 导入 matlab dll 
 
在 Vstest 上击右键,在弹出的菜单中选择 Add Reference 
 
 
按上图导入 test.dll 及 MWArray.dll, 
MWArray 路径如下(%matlabpath%\toolbox\dotnetbuilder\bin\win32\v2.0)  
 
在 c#中输入以下代码 
using MathWorks.MATLAB.NET.Utility; 
using MathWorks.MATLAB.NET.Arrays; 
using test; 
 
这样就完成了 dll 的引用,接下来使用 test 中的相关功能 
 
6、 先学习一些基本常识 
matlab 中的数据与 c#中数据的传递 
我们先了解一下 MWNumericArray,它是 MWArray 与 C#中数据传递的中间类。 
MWNumericArray 是 MWArray 和 c#中数据的中间类,怎么用?怎样在 C#与 matlab 间传递参数? 
a.double 型、int 型等数值类型的变量传递 
           MWNumericArray i = null, result = mydouble;  
           i=4; 
           myClass myclass = new myClass(); //实例化 
           result = (MWNumericArray)myclass.myfunc(i); 
b.字符串(需要用到 MWCharArray 和 MWArray 转换) 
MWCharArray FileName = myString; 
MWNumericArray sensitivity; 
sensitivity = (MWNumericArray)myClass.myAlgorithm((MWArray)FileName); 
c.多个输出参数组成的数组 
MWNumericArray out_Arr = (MWNumericArray)out_Args[1]; //取出第一个参数返回的数组
(matlab 返回的数组下界是从 1 开始的) 
取出数组中的一个元素值 
mydouble=out_Arr[i].ToScalarDouble(); 
如上面例子 ToScalarDouble 一类的 toXXXX 等方法还有许多。 
d.result.toArray 可以把 matlab 返回的矩阵变成 C#的 n x m 数组,eg: 
double[,] csArray= (double[,])result.ToArray(MWArrayComponent.Real); 
e.将数组传入 Matlab(和传递单个数值的方法一样) 
double[,] dbx = new double[2, 2] { { 1, 2 }, { 3, 4 } }; 
MWNumericArray x=dbx; 
myclass.picture(x); 
 
7、VS 测试程序 
建立以下窗口 
 
输入以下代码: 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using MathWorks.MATLAB.NET.Utility; 
using MathWorks.MATLAB.NET.Arrays; 
using test; 
 
 
namespace vstest 
{ 
    public partial class Form1 : Form 
    { 
 
       public  Array mm; 
        
        public Form1() 
        { 
            InitializeComponent(); 
 
 
 //输入队列 
            int[] x ={0,10,20,30,40,50,60,70,80,90,100,110,120, 
                       130,140,150,160,170,180,190,200,210,220, 
                       230,240,250,255}; 
//输出队列 
            int[] y ={34,35,36,37,38,40,43,45,48,52,58,66,74, 
                        83,92,105,115,129,149,168,185,198,212, 
                        220,223,224,224 }; 
           //0~255x轴队列 
            int[] p = new int[255]; 
            for (int i = 0; i < 255; i++) 
            { 
                p[i] = i; 
            } 
            int n = 6;  //多项式项数 
 
            //   int[] m = test.invgm();         //实例化 
            invgm mygam = new invgm(); 
            MWArray ResOut = null;             //用于值输出的matlab矩阵 
            ResOut = mygam.testfunc((MWNumericArray)x, (MWNumericArray)y, (MWNumericArray)p, 
(MWArray)n); 
            MWNumericArray temp = (MWNumericArray)ResOut; 
            //矩阵转换 
 
            mm = temp.ToArray(MWArrayComponent.Real); //c#规范的矩阵 
        }     
 
        private void button1_Click(object sender, EventArgs e) 
        { 
 
 
 //按textbox1的index找出相应的输出数据 
            int qq=Convert .ToInt16 ( textBox1.Text); 
            if (qq < 255 && qq>=0) 
            { 
                int pp = Convert.ToInt16(mm.GetValue(0, qq)); 
                textBox2.Text = pp.ToString(); 
            } 
            else 
            { 
                MessageBox.Show("data error"); 
            } 
        } 
    } 
} 
 
以上是 C#调用 matlab DLL 对一段数据曲线拟和后再作一个反拟和,并输出相应参数的例子。