MATLAB编译并调用C、C++程序.pdfVIP

  • 11
  • 0
  • 约2.67千字
  • 约 3页
  • 2017-05-01 发布于天津
  • 举报
MATLAB编译并调用C、C++程序.pdf

MATLAB 编译并调用 C、C++程序 MATLAB 的 C、C++程序编译器的设置: 在命令窗口中输入: mex -setup ,接着按照提示进行即可完成 C、 C++程序编译器的设置。 在 MATLAB 中编译 C、C++程序: C、C++程序的格式以 test.cpp 为例,如下示: #include mex.h //必须 #include math.h //程序功能实现函数 void Desc2Pol ( double dbX, double dbY, double *pdbA, double *pdbG ) { *pdbA = sqrt(dbX * dbX + dbY * dbY ); if (dbY == 0 ) { if ( dbX 0 ) { *pdbG = -90; } else { *pdbG = 90; } } else { *pdbG = atan( dbX/dbY ); } } //Matlab的接口函数,必须而且格式也一样 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { /* 其中nlhs为在Matlab环境中调用该函数时要求返回参数的个数 nrhs为在Matlab环境中调用该函数时输入参数的个数 plhs在Matlab环境中调用该函数时要求返回参数的队列 prhs在Matlab环境中调用该函数时输入参数的队列 */ int mrows = 0; int ncols = 0; double dbX = 0; double dbY = 0; double *pdbA = NULL; double *pdbG = NULL; /* Check for proper number of arguments. 检查输入、输出参数个数 */ //必须是2个输入参数 //包括 if ( nrhs != 2 ) { mexErrMsgTxt(Two input required.); } //必须是2个输出参数 //包括 if ( nlhs != 2 ) { mexErrMsgTxt(Two output required.); } /* Check for proper type of arguments . 检查输入、输出参数类型 */ mrows = mxGetM(prhs[0]); ncols = mxGetN(prhs[0]); if ( mxIsComplex(prhs[0]) || !(mrows == 1 ncols == 1)) { mexErrMsgTxt(The First input must be a noncomplex scalar double precision.); } mrows = mxGetM( prhs[ 1 ] ); ncols = mxGetN( prhs[ 1 ] ); if ( mxIsComplex( prhs[ 1 ] ) || !(mrows == 1 ncols == 1 ) ) { mexErrMsgTxt(The fourth input must be a noncomplex scalar double precision.); } dbX = mxGetScalar( prhs[ 0 ] ); dbY = mxGetScalar( prhs[ 1 ] ); /* Create matrix for the return argument.

文档评论(0)

1亿VIP精品文档

相关文档