⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 times2.c

📁 matlab从入门到精通第13章书后源码对初学者很有帮助
💻 C
字号:
// 编写C-MEX文件必须包含该头文件
#include "mex.h"

// C语言编写的计算子函数部分
void timestwo(double y[], double x[])
{
  y[0] = 2.0*x[0];
}

// C-MEX函数的接口函数
// int nlhs, mxArray *plhs[]为函数左侧的输出参数个数及参数矩阵
// int nrhs, mxArray *prhs[]为函数右侧的输入参数个数及参数矩阵
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]);
  
  /* 调用C语言计算子程序timestwo */
  timestwo(y,x);
}

⌨️ 快捷键说明

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