软件环境:windows Xp,VC6.0, Matlab 2010b
1、编写测试m函数,这个不变,即如下:
function [y,z] = myadd2(a, b) % dummy function, just to demonstrate the idea y = a+b; z = a+2*b; end
2、运行mbuild -setup设置好编译器后(我选的是vc6.0的编译器,没用MATLAB自带的),利用
mcc -W cpplib:libmyadd2 -T link:lib myadd2.m -C
3、VC下新建控制台程序,步骤如下:file>new,选择Win32 Console Application,右侧project name 填写
testmyadd2_r2010b
,点击OK。在下一个界面选“a simple application”finish点OK。至此程序框架搭好,将libmyadd2.dll,libmyadd2.h,libmyadd2.lib和libmyadd2.ctf复制到testmyadd2_r2010b工程目录下。
并将libmyadd2.h添加到Header files组内。
4、进行项目必要的设置(注意这里和VC2005有较大不同)。vc主界面进入project>Settings,点C/C++选项卡,Category选择Preprocessor,Additional include directories下填入如下路径
F:\MATLAB\R2010b\extern\include
libmyadd2.lib mclmcrrt.lib
两个库之间以及和其他库以空格隔开。然后Category选择input,Additional library path下填写:
F:\MATLAB\R2010b\extern\lib\win32\microsoft
5、打开testmyadd2_r2010b.cpp文件,在#include "stdafx.h"后添加
std::cout << "Hello world!" << std::endl; /* Initialize the MCR */ if( !mclInitializeApplication(NULL,0) ) { std::cout << "Could not initialize the application!" << std::endl; return -1; } // initialize lib if( !libmyadd2Initialize()) { std::cout << "Could not initialize libmyadd2!" << std::endl; return -1; } try { // declare and initialize a mwArray a(2, 2, mxDOUBLE_CLASS); double *aData; aData = new double[4]; int i; for( i=0; i<4; ++i) { aData[i] = 1.0*i; } // print output std::cout << "a = " << std::endl; std::cout << aData[0] << ",\t" << aData[1] << std::endl; std::cout << aData[2] << ",\t" << aData[3] << std::endl; a.SetData(aData, 4); // declare and initialize b mwArray b(2, 2, mxDOUBLE_CLASS); b(1,1) = 11.; b(1,2) = 12.; b(2,1) = 21.; b(2,2) = 22.; mwArray y(2, 2, mxDOUBLE_CLASS); mwArray z(2, 2, mxDOUBLE_CLASS); // call the function myadd2(2, y, z, a, b); // copy data from mwArray to C++ objects // allocate outputs double *yData, *zData; yData = new double[4]; if( yData == NULL ) { std::cout << "Failed to allocate memory for yData!" << std::endl; return -1; } zData = new double[4]; if( zData == NULL ) { std::cout << "Failed to allocate memory for zData!" << std::endl; return -1; } // copy data from mwArray to C++ y.GetData(yData, 4); z.GetData(zData, 4); // print output std::cout << "y = " << std::endl; std::cout << yData[0] << ",\t" << yData[1] << std::endl; std::cout << yData[2] << ",\t" << yData[3] << std::endl; std::cout << "z = " << std::endl; std::cout << zData[0] << ",\t" << zData[1] << std::endl; std::cout << zData[2] << ",\t" << zData[3] << std::endl; // deallocate memory delete [] aData; delete [] zData; delete [] yData; } catch( const mwException& e) { std::cerr << e.what() << std::endl; } // terminate the lib libmyadd2Terminate(); // terminate MCR mclTerminateApplication();
然后就ok了
6,里面最绕的莫过于mwArray
可以用下面的代码详细理解mwArray和一般数组之间的区别
mwArray a(3, 2, mxDOUBLE_CLASS); double *aData; aData = new double[6]; int iii; for( iii=0; iii<6; ++iii) { aData[iii] = iii+1; } // print output std::cout << "a = " << std::endl; std::cout << aData[0] << ",\t" << aData[1] << std::endl; std::cout << aData[2] << ",\t" << aData[3] << std::endl; std::cout << aData[4] << ",\t" << aData[5] << std::endl; a.SetData(aData, 6); std::cout << "a(1,1)="<<a(1,1)<<std::endl; std::cout << "a(1,2)="<<a(1,2)<<std::endl; std::cout << "a(2,1)="<<a(2,1)<<std::endl; std::cout << "a(2,2)="<<a(2,2)<<std::endl; std::cout << "a(3,1)="<<a(3,1)<<std::endl; std::cout << "a(3,2)="<<a(3,2)<<std::endl;运行结果如图:【一位数组到多维数组的组织方式是列优先的】
a =
1, 2
3, 4
5, 6
a(1,1)=1
a(1,2)=4
a(2,1)=2
a(2,2)=5
a(3,1)=3
a(3,2)=6