VS2008C#调用MATLAB进行混合编程
环境要求:XP SP2及以上、MATLAB2007a及以上、Visual Studio 2005及以上
本次个人实践的环境如下:
编程环境:Visual Studio 2008 C#.NET 、MATLAB R2009a
操作系统:Windows7 Ultimate
具体操作及图解如下:
1.首先搞清你想干什么,然后在matlab里面尽情的展示你的才华(当然你也可以在VS里面尽情展示,如果这样,那可以不必浪费您宝贵的时间了,我们在这儿是一定要进行混合编程的,废话不说了,下面开始正题!)
2.配置Matlab的C++编译环境:
在Matlab命令窗口中输入: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 SP1
[5] Microsoft Visual C++ 2008 Express
[6] Microsoft Visual C++ 2008 SP1
[0] None
Compiler: 6
(选择Microsoft Visual C++ 2008 SP1,如果在你的电脑上安装的是其他版本的VS,那么就选择自己有的那个版本即可)
The default location for Microsoft Visual C++ 2008 SP1 compilers is C:/Program Files/Microsoft Visual Studio 9.0,
but that directory does not exist on this machine.
Use C:/Program Files/Microsoft Visual Studio 9.0 anyway [y]/n? n
(是否将Vs2008安装到默认的安装目录下,这里选择n,以便处理没安装到这里的情况)
Please enter the location of your compiler: [C:/Program Files/Microsoft Visual Studio 9.0] C:/Program Files/Microsoft Visual Studio 9.0
(因为我个人是安装到默认目录下的,所以在这里输入默认安装目录,如果你不是这样,你就输入你安装目录即可)
Please verify your choices:
Compiler: Microsoft Visual C++ 2008 SP1
Location: C:/Program Files/Microsoft Visual Studio 9.0
Are these correct [y]/n? y
(确认上面的选择是否正确,正确则选择y,否则n)
****************************************************************************
Warning: Applications/components generated using Microsoft Visual Studio
2008 require that the Microsoft Visual Studio 2008 run-time
libraries be available on the computer used for deployment.
To redistribute your applications/components, be sure that the
deployment machine has these run-time libraries.
****************************************************************************
Trying to update options file:
C:/Users/administrator/AppData/Roaming/MathWorks/MATLAB/ R2009a/ compopts.bat
From template:
D:/PROGRA~1/MATLAB/R2009a/bin/win32/mbuildopts/msvc90compp.bat
Done . . .
当我们看到Done的时候,即表明配置工作顺利完成!
下面可以开始进行m函数的编写工作了。
3.编制将要调用的m函数(Matlab中)
这里我们为了示意整个混合编程的完整过程,仅编写了一个简单的矩阵相乘的实例,然后在VS中调用它来完成一个简单的运算:
下面是在matlab中编制的m函数文件的内容:multiply.m
function [z]=multiply(a,b)
z=a*b;
4.将m函数编译成.NET组件(Matlab中)
在Matlab命令窗口中输入:
>> deploytool
弹出的对话框如下:
我们依次点击File——New Deployment Project;
在弹出的对话框中,按照如下设置(选择MATLAB Mbuilder NE——.NET Component):
在下面的name框和location框中输入你自己的选择。
点击OK,在弹出的框中依次选择Project——Add File,如下图:
然后选择刚才我们编辑好的multiply.m文件,选择好后如下图:
接下来,可以开始编译m函数文件了,如图,依次选择Tools——Build:
到这里,我们就可以开始一段时间的休息,等着Matlab进行m函数文件的编译,在Deployment Tool Output框中显示如下:
Build output( 2010-07-21 22:57:30).
mcc -F 'E:/MatlabWork/multiply.prj'
mcc -W 'dotnet:multiply,Multiply,0.0,private' -d 'E:/MatlabWork/multiply/src' -T 'link:lib' -v 'class{Multiply:E:/MatlabWork/multiply.m}'
Compiler version: 4.10 (R2009a)
Processing include files...
2 item(s) added.
Processing directories installed with MCR...
The file E:/MatlabWork/multiply/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 40 path items.
Begin validation of MEX files: Wed Jul 21 22:58:43 2010
End validation of MEX files: Wed Jul 21 22:58:43 2010
Parsing file "E:/MatlabWork/multiply.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:/MatlabWork/multiply/src/Multiply.cs".
Generating file "E:/MatlabWork/multiply/src/MultiplyNative.cs".
Generating file "E:/MatlabWork/multiply/src/readme.txt".
Generating file "E:/MatlabWork/multiply/src/dotnet_mcc_component_data.cs".
Executing command: C:/Windows/Microsoft.NET/Framework/v2.0.50727/csc.exe /nologo /optimize /doc:"E:/MatlabWork/multiply/src/multiply.xml" /t:library /r:"d:/Program Files/MATLAB/R2009a/toolbox/dotnetbuilder/bin/win32/v2.0/MWArray.dll" /resource:"E:/MatlabWork/multiply/src/multiply.ctf" /out:"E:/MatlabWork/multiply/src/multiply.dll" "E:/MatlabWork/multiply/src/Multiply.cs" "E:/MatlabWork/multiply/src/dotnet_mcc_component_data.cs"
Executing command: C:/Windows/Microsoft.NET/Framework/v2.0.50727/csc.exe /nologo /optimize /doc:"E:/MatlabWork/multiply/src/multiplyNative.xml" /t:library /r:"d:/Program Files/MATLAB/R2009a/toolbox/dotnetbuilder/bin/win32/v2.0/MWArray.dll" /resource:"E:/MatlabWork/multiply/src/multiply.ctf" /out:"E:/MatlabWork/multiply/src/multiplyNative.dll" "E:/MatlabWork/multiply/src/MultiplyNative.cs" "E:/MatlabWork/multiply/src/dotnet_mcc_component_data.cs"
Copying: E:/MatlabWork/multiply/src/multiply.dll -> E:/MatlabWork/multiply/distrib
Copying: E:/MatlabWork/multiply/src/multiplyNative.dll -> E:/MatlabWork/multiply/distrib
Copying: E:/MatlabWork/multiply/src/readme.txt -> E:/MatlabWork/multiply/distrib
Compilation completed successfully. The output is located in E:/MatlabWork/multiply/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).
到这里就完成了m函数文件的编译,我们接下来要介绍怎么在VS C#中调用我们编译好的动态连接库(dll)文件,在我们m函数文件的目录下,有一个同名的文件夹,里面的distrib文件夹下有一个multiply.dll文件,这就是我们在接下来的步骤里面需要用到的dll文件。
5.VS2008C#中调用编译好的dll文件
首先在VS中新建一个控制台应用程序,然后必须做的两件重要事情如下:
(1)添加引用(添加上一步生成的multiply.dll文件)
(2)添加引用(MWArray.dll)
这个dll文件位于你安装Matlab的文件夹下,具体的路径请各位参照下面我个人的路径
D:/Program Files/MATLAB/R2009a/toolbox/dotnetbuilder/bin/win32/v2.0
MWArray.dll是matlab提供的一个.net类库,里面主要定义了一种矩阵格式的数据形式。
(3)在命名空间中加入:
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using multiply;
(4)下面是我的测试程序的源代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using multiply;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MWNumericArray aa = new double[2, 2] { { 1, 2 }, { 3, 4 } };
MWNumericArray bb = new double[2, 2] { { 1, 2 }, { 3, 4 } };
MWArray cc;
multiply.Multiply mulob = new multiply.Multiply();//引用dll,初始化一个对象,以便调用其函数
cc = mulob.multiply(aa,bb);//调用函数
Console.WriteLine(cc);
Console.ReadLine();
}
}
}
(5)最终的结果如下图:
6.实验总结:
经过本次混合编程,完全就C#调用Matlab进行了一次完整的演示,其中问题还有很多:比如:(1)仅对将m函数编译成.NET组件进行了实验,未对COM组件进行;
(2)具体的关于C#和Matlab进行矩阵交换数据的方法和内容没有具体讲解,大家可以参考其他混合编程关于矩阵运算的实例;
(3)C#调用Matlab混合编程效率,实在是不敢恭维,小测了一把,本程序需要花费至少3s,等待过程难耐,希望以后各位能在运行效率上下功夫;
(4)仅完成了调用m函数功能,不能执行单个的脚本命令,这一点在初步的研究后,得出可以使用Matlab Engine来执行单个的脚本命令;
(5)C#调用Matlab混合编程,在已经安装好运行环境的电脑上能一蹴而就,但是在其他人的机子上使用程序时,则需要另外附加安装.netframwork与MCR,这一点请各位一定要注意(并不是一次编译,多台机子都能立即运行),我个人的理解:混合编程的优势在于快速测试,而不在于效率和商用;
这是鄙人初次写作博客,这篇博客也是一个初学者的首演,希望大家多提建议,我们共同将这种方法完善!谢谢!