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

📄 timestwo.c

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

void timestwo(double y[], double x[])
{
  y[0] = 2.0*x[0];   /* 计算子程序,完成实际计算:y = 2.0×x  */
}

/* 编辑mexFucntion入口子程序,格式是固定的  */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
  double *x,*y;           /* 为输入 输出参数创建变量,类型为指针  */
  mwSize mrows,ncols;     /* 输入参数的行数和列数(MATLAB的数据类型默认为矩阵)*/

  /* 检查输入/输出参数个合法性 */
  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) ) {
    /* 输入参数若不是double型,或是复数,或不是标量则报错 */
    mexErrMsgTxt("Input must be a noncomplex scalar double.");
  }
  
  /* 为输出参数创建变量 */
  plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
  
  /*为计算子程序的输入参数赋值,值为指向入口子程序参数prhs[0]的指针 */
  x = mxGetPr(prhs[0]);
  /* 为计算子程序的输出参数赋值,值为指向刚创建的输出参数变量plhs[0]的指针 */
  y = mxGetPr(plhs[0]);
  
  /* 设置好输入/输出参数后,就可以调用计算子程序timestwo了*/
  /* 执行y = 2*x操作,并返回结果y的值 */
  timestwo(y,x);
}

⌨️ 快捷键说明

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