基于VS2010和Matlab R2010b版本的混合编程的实现
来源:互联网 发布:day of defeat mac 编辑:程序博客网 时间:2024/06/03 21:23
为了更加直观的说明如何实现混合编程,笔者写了一个简单的程序作为测试。
先在Matlab里面写一个M文件,代码如下:
function mywaveletclear 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;
}
接下来直接编译执行程序就可以
- 基于VS2010和Matlab R2010b版本的混合编程的实现
- 基于VS2008和Matlab R2010b版本的混合编程的实现(转自:http://blog.sina.com.cn/s/blog_49cb42490100nm5j.html)
- 64位VS2012+64位matlab R2010b和32位VS2012+32位matlab R2010b 使用matlab engine实现混合编程配置
- win7_32位系统下,VS2008与MATLAB R2010b的混合编程(绘图)
- MATLAB和VS2010的混合编程需要注意的问题
- 基于MATLAB 进行混合编程的接口技术实现
- 利用Matcom实现基于MATLAB的混合编程
- 基于COM组件实现C++与Matlab的混合编程
- 利用Matlab引擎实现VC和Matlab的混合编程
- matlab-VC混合编程之matlab低版本,VS高版本,mex -setup,mbuild -setup都找不到VS2010的编
- 基于MATLAB与C++的混合编程
- VC环境和Matlab混合编程的实现方法
- vs2010和Matlab R2012a 混合编程
- vs2010和Matlab R2012b 混合编程
- 记VS2010与MATLAB混合编程遇到的一些问题
- vc和matlab的混合编程
- MFC和Matlab的混合编程入门
- 基于VC与Matlab的混合编程实现图像的三维显示
- Messenger与AIDL的异同
- Android studio导入另外一个项目作为Library后出现两个启动项目等各类问题
- 如何避免新入职的程序员痛苦和迷茫?
- 内购 税务信息页填写-新版填写
- HTTP状态码大全
- 基于VS2010和Matlab R2010b版本的混合编程的实现
- 头部下拉刷新控件使用
- [RK3399] Type-C 驱动流程分析
- 英文界面的Windows系统中解决 Sourceinsight 中的中文注释显示乱码的问题
- DB2 JRE8 连接异常
- Other linker flags
- Struts2框架学习(第一讲)
- 创建者模式
- Kubernetes(K8s)容器设计模式实践案例 单节点多容器模式