基于VS2010和Matlab R2010b版本的混合编程的实现

来源:互联网 发布:day of defeat mac 编辑:程序博客网 时间:2024/06/03 21:23
        众所周知Matlab的强大运算能力让各种工程软件望而却步,而VC程序的友好界面又让人爱不释手,如果能够让两者的优势结合起来的话,势必能让程序员欢欣鼓舞。还好MathWorks已经为我们想到了,基本上现在市面上主流的Matlab版本都可以支持VC和Matlab的混合编程。但是目前网上资料基本上都是基于Matlab 6.0和VC 6.0的,和新版本的实现方法有所不同,因此笔者通过不断的尝试和研究,终于调试通了基于VS2010和Matlab R2011b的混合编程。

为了更加直观的说明如何实现混合编程,笔者写了一个简单的程序作为测试。

先在Matlab里面写一个M文件,代码如下:

function mywavelet
clear all;
a=[100:900];
b=sqrt(a);
plot(a,b);
end
将M文件保存为mywavelet.m,保存在F:/test文件夹中。然后在Matlab主窗口中将当前文件夹改为F:/test:
cd F:/test;

为了能够让VC调用Matlab程序,我们可以采用将Matlab程序包装成动态链接文件DLL的形式,然后让VC调用动态链接文件,因此在Matlab中,我们需要设置一下Matlab的编译库,在Matlab主窗口中键入如下代码:

>> mbuild -setup
 
Welcome to mbuild -setup.  This utility will help you set up  
a default compiler.  For a list of supported compilers, see  
http://www.mathworks.com/support/compilers/R2011b/win64.html 
 
Please choose your compiler for building standalone MATLAB applications: 
 
Would you like mbuild to locate installed compilers [y]/n? y
 
Select a compiler: 
[1] Microsoft Visual C++ 2010 in C:\Program Files (x86)\Microsoft Visual Studio 10.0 
 
[0] None 
 
Compiler: 1
 
Please verify your choices: 
 
Compiler: Microsoft Visual C++ 2010  
Location: C:\Program Files (x86)\Microsoft Visual Studio 10.0 
 
Are these correct [y]/n? y
 
**************************************************************************** 
  Warning: Applications/components generated using Microsoft Visual C++      
           2010 require that the Microsoft Visual Studio 2010 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\R2011b\compopts.bat 
From template:              D:\PROGRA~1\MATLAB\R2011b\bin\win64\mbuildopts\msvc100compp.bat 
 
Done . . . 
 
>> mex -setup
 
Welcome to mex -setup.  This utility will help you set up  
a default compiler.  For a list of supported compilers, see  
http://www.mathworks.com/support/compilers/R2011b/win64.html 
 
Please choose your compiler for building MEX-files: 
 
Would you like mex to locate installed compilers [y]/n? y
 
Select a compiler: 
[1] Microsoft Visual C++ 2010 in C:\Program Files (x86)\Microsoft Visual Studio 10.0 
 
[0] None 
 
Compiler: 1
 
Please verify your choices: 
 
Compiler: Microsoft Visual C++ 2010  
Location: C:\Program Files (x86)\Microsoft Visual Studio 10.0 
 
Are these correct [y]/n? y
 
*************************************************************************** 
  Warning: MEX-files generated using Microsoft Visual C++ 2010 require 
           that Microsoft Visual Studio 2010 run-time libraries be  
           available on the computer they are run on. 
           If you plan to redistribute your MEX-files to other MATLAB 
           users, be sure that they have the run-time libraries. 
*************************************************************************** 
 
 
Trying to update options file: C:\Users\Administrator\AppData\Roaming\MathWorks\MATLAB\R2011b\mexopts.bat 
From template:              D:\PROGRA~1\MATLAB\R2011b\bin\win64\mexopts\msvc100opts.bat 
 
Done . . . 

如果出现以上说明,则证明Matlab编译器设置成功了。接下来是生成M文件的DLL文件。
在Matlab主窗口中键入如下代码:

 mcc -W cpplib:MyDLL -T link:lib mywavelet.m 

其中cpplib:后面的是需要生成文件的文件名,是自己取的,link:lib后面的MyFunc.m是转换为DLL的M文件的文件名。 -W/-T/-C是参数,具体含义可以通过mcc –help命令查看,注意参数的大小写。

Matlab编译后会在MyFunc.m所在文件夹下生成9个文件,分别是:
mccEcxludedFiles.log
MyDLL.cpp
MyDLL.ctf
MyDLL.dll
MyDLL.exp
MyDLL.exports
MyDLL.h
MyDLL.lib
readme.txt
其中MyDLL.dll,MyDLL.lib,MyDLL.ctf,MyDLL.h是我们需要的文件,至此,Matlab方面就已经设置妥当了。

我们需要的只有三个文件,.dll、.lib、.h文件,其他的可要可不要。好了,源文件已经编译好了,接下来就是对VC环境进行配置了,再次声明,我的平台是VC2010+Matlab2011b,首先创建一个工程,然后右键单击工程—>属性—>VC++目录,添加包含目录:D:\Program Files\MATLAB\R2011b\extern\include,然后添加库目录:D:\Program Files\MATLAB\R2011b\extern\lib\win64\microsoft。注意,matlab的库目录,这个VC++目录就设置好了,接下来还是在属性这里面,选择链接器—>输入,添加附加依赖项:mclmcrrt.lib、MyDLL.lib,注意,每个库文件占一行,这样属性就配置好了,接下来就是对文件的引用,将编译得到的三个文件复制(三个一起复制)复制到你的工程文件下面,如果你不清楚复制在哪一个目录下面,就复制在你工程文件的每一个下面吧(正确的方式是:.h文件复制到你的C++源文件目录下面,.lib和.dll文件复制到你的可执行文件中,也就是debug文件夹中,如果怕出错就全部复制),接下来就在C++源文件中添加头文件#include"MyDLL.h",下面我将整个程序给出来,如下:

#include <iostream>
#include "add.h"
using namespace std;
int main()
{
 if(addInitialize())    //初始化,这一步是必须的
 {
  cout<<"success!!!"<<endl;
 }
 else
 {
  cout<<"fail!!!"<<endl;
  return 0;
 }
 //定义3个mwArray类型变量,用于存放矩阵
 mwArray a(2,3,mxDOUBLE_CLASS); 
    mwArray b(2,3,mxDOUBLE_CLASS);
 mwArray c(2,3,mxDOUBLE_CLASS); 
 double x[] = {1,2,3,4,5,6};
 double y[] = {7,8,9,10,11,12};
 double *sum=new double[6];        //注意这个是*类型的
 // 给输入 mxArray 对象赋值
 a.SetData(x,6);
 b.SetData(y,6);
 //调用DLL函数
 add(1,c,a,b);
 //获取结果
 c.GetData(sum,6);
 
 //输出结果
 for (int i=0;i<2;i++)
 {
  for (int j=0;j<3;j++)
   cout<<sum[j*2+i]<<" ";
  cout<<endl;
 }
 
 cout<<"it is successful !"<<endl;
 return 0;
}

接下来直接编译执行程序就可以

0 0