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

📄 timestwoalt.c

📁 matlab实用教程
💻 C
字号:
#include "mex.h"

void timestwo_alt(double *y, double x)
{
  *y = 2.0*x;
}

void mexFunction( int nlhs, mxArray *plhs[],
                int nrhs, const mxArray *prhs[] )
{
  double *y;
  double  x;
  
 /* 检查参数 */
  if (nrhs != 1) { 
    mexErrMsgTxt("One input argument required."); 
  } else if (nlhs > 1) {
    mexErrMsgTxt("Too many output arguments."); 
  } else if (!mxIsNumeric(prhs[0])) {
    mexErrMsgTxt("Argument must be numeric.");
  } else if (mxGetNumberOfElements(prhs[0]) != 1 || mxIsComplex(prhs[0])) {
    mexErrMsgTxt("Argument must be non-complex scalar.");
  }
  /* 为输出参数创建变量 */
  plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
 
   /* 
     为参数 x、y赋值,x为值,而y为指针
    (由于MATLAB没有值传递,所以用指针才能得到修改后的y值,
     不然修改的是y的一个副本,为临时变量,在函数返回时,y值没有变化,
     不能得到希望的结果)
   */
  x = mxGetScalar(prhs[0]);
  y = mxGetPr(plhs[0]);
  
  /* 调用timestwo_alt 子函数 */
  timestwo_alt(y,x);
}

⌨️ 快捷键说明

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