- 积分
- 149
- 注册时间
- 2003-5-22
- 仿真币
-
- 最后登录
- 1970-1-1
|
发表于 2006-3-10 21:11:22
|
显示全部楼层
来自 新加坡
Re:请教董维国老师:关于mxArray
再贴上C++语言版本tstfhdl.cpp例程,以供参考:- #include <math.h>
- #include "mex.h"
- void mexFunction(int nlhs, mxArray *plhs[],
- int nrhs, const mxArray *prhs[])
- {
- int i, j, m, n;
-
- /* 输入变量为2,一个是函数句柄,另一个是参数*/
- if (nrhs != 2)
- mexErrMsgTxt("The number of input must be two!");
- /*输出变量数目可以为0或者1*/
- if (nlhs > 1)
- mexErrMsgTxt("The number of output must be zero or one!");
-
- /* 第一个输入必须是函数句柄 */
- if ( !mxIsClass(prhs[0], "function_handle") )
- {
- mexErrMsgTxt("The first input must be a function handle!");
- }
-
- /* 得到输入矩阵的大小 */
- m = mxGetM(prhs[1]);
- n = mxGetN(prhs[1]);
-
- /* 创建输出变量所对应的mxArray */
- plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);
- /* 使用函数句柄的第一种方法:调用mexEvalString */
- const mxArray *funHandle = prhs[0];
- const mxArray *xx = prhs[1];
- mexPutVariable("base", "xx", xx);
- mexPutVariable("base", "funHandle", funHandle);
- mexEvalString("yy = feval(funHandle, xx);");
- plhs[0] = mexGetVariable("base", "yy");
- /* 使用函数句柄的第二种方法:调用mexCallMATLAB */
- /*
- mexCallMATLAB(1, &plhs[0], 2, prhs, "feval");
- */
- }
复制代码 |
|