例11-6.m

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

M
42
字号
#include <string.h> /* Needed for memcpy() */
#include "mex.h"

#define NDIMS 2
#define TOTAL_ELEMENTS 4

/* 计算功能子程序 */
void doubleelement (unsigned short *x)
{
  unsigned short scalar=2;
  int i,j;

  for (i=0; i<2; i++) {
    for (j=0; j<2; j++) {
      *(x+i+j) = scalar * *(x+i+j);
    }
  }
}


/* 入口代码子程序 */
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
  const int dims[]={2,2};
  unsigned char *start_of_pr;
  unsigned short data[]={1,2,3,4};
  int bytes_to_copy;

  /* 调用计算功能子程序 */
  doubleelement (data);

  /* 创建2*2的无符号16比特位整型数数组 */
  plhs[0] = mxCreateNumericArray(NDIMS,dims,mxUINT16_CLASS,
                                  mxREAL);

  /* 获得数组的实部 */
  start_of_pr = (unsigned char *)mxGetData(plhs[0]);
  bytes_to_copy = TOTAL_ELEMENTS * mxGetElementSize(plhs[0]);
  memcpy(start_of_pr, data, bytes_to_copy);
}

⌨️ 快捷键说明

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