Matlab2012a与VS2010(C)混合编程课件
Matlab2012a与VS2010(C++)混合编程经历了各种报错和崩溃,用了近1个月的时间终于把C++的程序调通了。在编程过程中,我在网上找了各种Matlab和C++混编的资料,感觉混编的资料太少,故将自己编程的过程和心得分享给大家,希望能帮到和我一样挣扎在混编领域中的同学。环境:windows8.1,Matlab2012a,VS2010需要编的C++程序要实现与Matlab一样的功能,从键盘输入数据,获得计算结果和图像。如下图:大概的过程:Matlab的m文件→dll→C++调用dll和Matlab中的engine(与图像有关,后面会详细说明)→输出计算结果和图像1.Matlab编译器设置(电脑安装有VS2010)(1)mex命令设置(a) 运行 Matlab ,在 Matlab 的命令窗口 (Command Window) 键入“ mex -setup ”命令后,按回车键,安装 Matlab 编译器;(b) 命令窗口出现如下提示:Please choose your compiler for building external interface (MEX) files:Would you like mex to locate installed compilers [y]/n?此时键入“y” ,按回车;(c) 命令窗口出现如下提示:Please choose your compiler for building external interface (MEX) files:Would you like mex to locate installed compilers [y]/n? ySelect a compiler:[1]Microsoft Visual Studio 2010 in G:\ Visual Studio 2010[0] None此时键入 ” 1” ,按回车 ;选择 Microsoft Visual Studio 2010 的编译器(d) 命令窗口出现如下提示:Please verify your choices:Compiler: Microsoft Visual Studio 2010Location: G:\ Visual Studio 2010Are these correct?([y]/n):此时键入 ”y” ,按回车 ;确认选择 Microsoft Visual Studio 2010 的编译器(2) mbuild 命令设置(a) 运行 Matlab ,在 Matlab 的命令窗口 (Command Window) 键入“ mbuild -setup ”命令后,按回车键,安装 Matlab 编译器;(b) 命令窗口出现如下提示:Please choose your compiler for building standalone MATLAB applications:Would you like mbuild to locate installed compilers [y]/n?此时键入 ”y” ,按回车 ;(c) 命令窗口出现如下提示:[1] Microsoft Visual Studio 2010 in G:\ Visual Studio 2010 [0] None此时键入 “1” ,按回车 ;选择 Microsoft Visual Studio 2010 的编译器(d) 命令窗口出现如下提示:Please verify your choices:Compiler: Microsoft Visual Studio 2010Location: G:\ Visual Studio 2010Are these correct?([y]/n):此时键入 ”y” ,按回车 ,确认选择 Microsoft Visual Studio 2010 的编译器,编译器设置完成。2.将 Matlab 函数转成 DLL 函数(tips:能封装成dll文件的m文件其内容必须是自定义函数形式)函数文件 RE_1.m ,内容如下:function [f0,f1,f2]=RE_1(S0,S1,S2)x = 0:0.01:2*pi;r =cos(3*x)/5 - (3*cos(6*x))/5 + (7*cos(20*x))/10 + sin(2*x)/10 + (3*sin(13*x))/10 + 10;r_max = max(r);r_min = min(r);r1=r_max;r2=r_min;f0=r1-r2;N=128;p1=19;p2=64;c0=1;c1=-sin(2*pi*p2/N)/sin(2*pi*(p2-p1)/N);c2=sin(2*pi*p1/N)/sin(2*pi*(p2-p1)/N);