例11-2.m

来自「这是一个MATLAB7.0基础与提高例题的所有源码」· M 代码 · 共 36 行

M
36
字号
#include "mex.h"
//计算功能子程序
void timestwo(double y[], double x[])
{
  y[0] = 2.0*x[0];
}

//入口子程序
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
                 const mxArray *prhs[])
{
  double *x, *y;
  int mrows, ncols;
//检查变量个数  
  if (nrhs != 1) {
    mexErrMsgTxt("One input required.");
  } else if (nlhs > 1) {
    mexErrMsgTxt("Too many output arguments");
  }
  mrows = mxGetM(prhs[0]);
  ncols = mxGetN(prhs[0]);
  if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
      !(mrows == 1 && ncols == 1)) {
    mexErrMsgTxt("Input must be a noncomplex scalar double.");
  }
  //创建矩阵作为返回参数
  plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
  
  //为每一个输入输出参数分配指针
  x = mxGetPr(prhs[0]);
  y = mxGetPr(plhs[0]);
  
  //调用timestwo计算子程序
  timestwo(y,x);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?