VS2005 与 Matalab R2007b(7.5)混合编程 总结
一、 环境配置
1)编程环境
操作系统:Windows XP;
开发工具:VS2005(Visual C++ 2005) ,MATLAB R2007b(7.5);
安装路径:VS2005 C:/Program Files/Microsoft Visual Studio 8 ; Matalab7.5 D:/Program Files/MATLAB/R2007b
注意:安装时,要先装VS2005后装MATLAB R2007b,因为MATLAB R2007b要用到Visual C++ 2005中的.NET框架。
起初,我的matalab是6.5版本的,然后进行环境配置的时候怎么也找不到vc++2005,查阅了很多资料之后,我发现应该6.5版本太低了,不支持和vs2005混编。
2)配置编译器
使用 mbuild -setup和 mex -setup两个命令对编译器进行配置,两个命令均选择 Microsoft Visual C++ 2005。
注意:mbuild(mex)和 –setup 之间有一个空格;没有空格就会出错!
1. mbuild –setup
>> mbuild -setup
Please choose your compiler for building standalone MATLAB applications:
Would you like mbuild to locate installed compilers [y]/n? y
Select a compiler:
[1] Lcc-win32 C 2.4.1 in D:/PROGRA~1/MATLAB/R2007b/sys/lcc
[2] Microsoft Visual C++ 2005 in C:/Program Files/Microsoft Visual Studio 8
[0] None
Compiler: 2
Please verify your choices:
Compiler: Microsoft Visual C++ 2005
Location: C:/Program Files/Microsoft Visual Studio 8
Are these correct?([y]/n): y
****************************************************************************
Warning: Applications/components generated using Microsoft Visual Studio
2005 require that the Microsoft Visual Studio 2005 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. More information
is at:
****************************************************************************
Trying to update options file: C:/Documents and Settings/Administrator/Application Data/MathWorks/MATLAB/R2007b/compopts.bat
From template: D:/PROGRA~1/MATLAB/R2007b/bin/win32/mbuildopts/msvc80compp.bat
Done . . .
2.mex –setup
- >> mex -setup
- MEX –setup,与mbuild 配置方法相同。
复制代码
这样,matlab 编译环境配置成功了。
虽然,这里有warning,但是不影响,可以继续进行后面的操作。我试图解决warning 及它下面的错误,照样从网站上下了vcredist_x86.exe然后进行安装;并且将msvc80opts.bat从D:/PROGRA~1/MATLAB/R2007b/bin/win32/mexopts/拷到C:/Documents and Settings/Administrator/Application Data/MathWorks/MATLAB/R2007b 替换mexopts.bat,但是都不管用,重新执行上述两个编译指令进行编译,仍然是有这样的错误,至今不得解!由于不影响后来的操作,无奈,我暂时放弃这个疑团!
3. vs2005 环境配置
第一步:工具->选项->项目和解决方案->vc++目录
显示以下内容的目录:包含文件 添加 MATLABROOT/R2007b/extern/include
显示以下内容的目录:库文件 添加 MATLABROOT/R2007b/extern/include MATLABROOT /R2007b/extern/lib/win32/microsof
第二步:项目->项目名字属性页 ->C/C++ ->常规 附加包含目录
MATLABROOT/R2007b/extern/include/win32
链接器->常规 附加库目录 MATLABROOT/R2007b/extern/include/win32/microsoft
链接器->输入 附加依赖项 添加 libfxtu.lib mclmcrrt.lib
二、 程序实现
1)matalab程序文件名字为 : fxtu.m
a.编写程序
- function beam = fxtu(w1,w2)
- m=4; % sensors
- n=2; % sources
- d=1/2; % 1/2 lambada
- L=100; % resolution in [-90' 90']
- %w1 = [0.2400 + 0.0019i 0.2400 - 0.0005i 0.2400 - 0.0019i 0.2400 + 0.0019i ];
- %w2 = [0.2400 + 0.0019i 0.2400 - 0.0005i 0.2400 - 0.0019i 0.2400 + 0.0019i ];
- beam=zeros(1,L);
- for i = 1 : L
- a=exp(-j*2*pi*d*[0:m-1].'*sin( -pi/2+ pi*(i-1)/L));
- beam(i)=20*log10(abs(w1*a)+abs(w2*a));
- end
- % plotting command followed
- %figure
- angle=-90:180/L:(90-180/L);
- %plot(angle,beam);
- polar(angle *pi /180, beam +50);
- xlabel('angle');
- ylabel('power dbm');
- end
复制代码
b. 执行程序
我们执行如下语句将matalab的fxtu 函数编译成一个c++接口的DLL:
mcc -W cpplib:libfxtu -T link:lib fxtu.m
执行结果,我们会得到如下一系列文件:
我们只需要libfxtu.h libfxtu.dll libfxtu.lib libfxtu.ctf 四个文件!
注意:
1. 执行上述命令 产生的文件在你所设置的current directory 目录里面,如下
2. 如果你执行该指令时current directory 设置为work D:/Program Files/MATLAB/R2007b/work.就会出现如下错误:
Warning: Duplicate directory name: D:/Program Files/MATLAB/R2007b/work.
3. 这个并不妨碍,只要改一下目录就可以了,我的改成D:/Program Files/MATLAB/R2007b/work.
那么你执行命令所产生的一系列文件就会在这个文件夹里面。
2) VS2005程序
这里我用的是MFC对话框程序。
a.编写程序
建MFC对话框工程,将matalab mcc编译的libfxtu.h libfxtu.dll libfxtu.lib libfxtu.ctf四个文件拷入工程目录中,并添加到工程中去。其中相关代码主要有:
类视图 Cp3APP 中的 InitInstance(void)中添加如下代码:
- if(!mclInitializeApplication(NULL,0))
- {
- AfxMessageBox(_T("不能初始化应用程序!"));
- return TRUE;
- }
- if(!libfxtuInitialize() )
- {
- AfxMessageBox(_T("不能初始化库!"));
- return TRUE;
- }
复制代码
类视图 Cp3APP 中的ExitInstance() 代码如下:
- int Cp3App::ExitInstance()
- {
- // TODO: 在此添加专用代码和/或调用基类
- libfxtuTerminate();
- mclTerminateApplication();
- return CWinApp::ExitInstance();
- }
复制代码
对话框按钮操作:
- void Cp3Dlg::OnBnClickedButton4()
- {
- // TODO: 在此添加控件通知处理程序代码
- CString r_qz1,r_qz2,r_qz3,r_qz4,r_qz5,r_qz6,r_qz7,r_qz8;//r 实部
- CString i_qz1,i_qz2,i_qz3,i_qz4,i_qz5,i_qz6,i_qz7,i_qz8;//i 虚部
- GetDlgItemText(IDC_EDIT13,r_qz1);//权值
- GetDlgItemText(IDC_EDIT14,r_qz2);//权值
- GetDlgItemText(IDC_EDIT15,r_qz3);//权值
- GetDlgItemText(IDC_EDIT16,r_qz4);//权值
- GetDlgItemText(IDC_EDIT17,r_qz5);//权值
- GetDlgItemText(IDC_EDIT18,r_qz6);//权值
- GetDlgItemText(IDC_EDIT19,r_qz7);//权值
- GetDlgItemText(IDC_EDIT20,r_qz8);//权值
- GetDlgItemText(IDC_EDIT21,i_qz1);//权值
- GetDlgItemText(IDC_EDIT22,i_qz2);//权值
- GetDlgItemText(IDC_EDIT23,i_qz3);//权值
- GetDlgItemText(IDC_EDIT24,i_qz4);//权值
- GetDlgItemText(IDC_EDIT25,i_qz5);//权值
- GetDlgItemText(IDC_EDIT26,i_qz6);//权值
- GetDlgItemText(IDC_EDIT27,i_qz7);//权值
- GetDlgItemText(IDC_EDIT28,i_qz8);//权值
- float d_r_qz1,d_r_qz2,d_r_qz3,d_r_qz4,d_r_qz5,d_r_qz6,d_r_qz7,d_r_qz8;
- float d_i_qz1,d_i_qz2,d_i_qz3,d_i_qz4,d_i_qz5,d_i_qz6,d_i_qz7,d_i_qz8;
- d_r_qz1 = (float)atof((char *)(LPTSTR)(LPCTSTR)r_qz1);
- d_r_qz2 = (float)atof((char *)(LPTSTR)(LPCTSTR)r_qz2);
- d_r_qz3 = (float)atof((char *)(LPTSTR)(LPCTSTR)r_qz3);
- d_r_qz4 = (float)atof((char *)(LPTSTR)(LPCTSTR)r_qz4);
- d_r_qz5 = (float)atof((char *)(LPTSTR)(LPCTSTR)r_qz5);
- d_r_qz6 = (float)atof((char *)(LPTSTR)(LPCTSTR)r_qz6);
- d_r_qz7 = (float)atof((char *)(LPTSTR)(LPCTSTR)r_qz7);
- d_r_qz8 = (float)atof((char *)(LPTSTR)(LPCTSTR)r_qz8);
- d_i_qz1 = (float)atof((char *)(LPTSTR)(LPCTSTR)i_qz1);
- d_i_qz2 = (float)atof((char *)(LPTSTR)(LPCTSTR)i_qz2);
- d_i_qz3 = (float)atof((char *)(LPTSTR)(LPCTSTR)i_qz3);
- d_i_qz4 = (float)atof((char *)(LPTSTR)(LPCTSTR)i_qz4);
- d_i_qz5 = (float)atof((char *)(LPTSTR)(LPCTSTR)i_qz5);
- d_i_qz6 = (float)atof((char *)(LPTSTR)(LPCTSTR)i_qz6);
- d_i_qz7 = (float)atof((char *)(LPTSTR)(LPCTSTR)i_qz7);
- d_i_qz8 = (float)atof((char *)(LPTSTR)(LPCTSTR)i_qz8);
- /*if (!IsDouble(r_qz1) || !IsDouble(r_qz2) || !IsDouble(r_qz3) || !IsDouble(r_qz4)/
- || !IsDouble(r_qz5) || !IsDouble(r_qz6) || !IsDouble(r_qz7) || !IsDouble(r_qz8) /
- || !IsDouble(i_qz1) || !IsDouble(i_qz2) || !IsDouble(i_qz3) || !IsDouble(i_qz4) /
- || !IsDouble(i_qz5) || !IsDouble(i_qz6) || !IsDouble(i_qz7) || !IsDouble(i_qz8) )
- {
- AfxMessageBox(_T("输入格式不正确,请重新输入!"));
- return;
- }
- */
- try
- {
- double data1[4];//w1 real
- double data2[4];//w2 real
- double data3[4];//w1 imag
- double data4[4];//w2 imag
- data1[0] = d_r_qz1;
- data1[1] = d_r_qz2;
- data1[2] = d_r_qz3;
- data1[3] = d_r_qz4;
-
- data2[0] = d_r_qz5;
- data2[1] = d_r_qz6;
- data2[2] = d_r_qz7;
- data2[3] = d_r_qz8;
- data3[0] = d_i_qz1;
- data3[1] = d_i_qz2;
- data3[2] = d_i_qz3;
- data3[3] = d_i_qz4;
- data4[0] = d_i_qz5;
- data4[1] = d_i_qz6;
- data4[2] = d_i_qz7;
- data4[3] = d_i_qz8;
- mwArray w1(1,4,mxDOUBLE_CLASS,mxCOMPLEX);
- mwArray w2(1,4,mxDOUBLE_CLASS,mxCOMPLEX);
- w1.Real().SetData(data1,4);
- w1.Imag().SetData(data3,4);
- w2.Real().SetData(data2,4);
- w2.Imag().SetData(data4,4);
- mwArray beam;
- fxtu(1,beam,w1,w2);
- }
- catch (const mwException& e)
- {
- CString error(e.what());
- MessageBox(error);
- }
- catch (...)
- {
- MessageBox(_T("错误!"));
- }
- }
复制代码
b.编译程序
执行结果:
注意:
这里,我们的matalab产生的dll 函数 输入是复数矩阵;关于mwArray 类的操作,我们可以看它的声明。
- mwArray Imag() - Return mwArray that references imaginary part of complex array
- C++ Syntax
- #include "mclcppclass.h"
- double rdata[4] = {1.0, 2.0, 3.0, 4.0};
- double idata[4] = {10.0, 20.0, 30.0, 40.0};
- mwArray a(2, 2, mxDOUBLE_CLASS, mxCOMPLEX);
- a.Real().SetData(rdata, 4);
- a.Imag().SetData(idata, 4);
|
|