doubleelement.c

来自「有关matlab r2007的实用教程」· C语言 代码 · 共 54 行

C
54
字号
#include <string.h> /* needed for memcpy() */
#include "mex.h"

/*
 * doubleelement.c - example found in API guide
 *
 * constructs a 2-by-2 matrix with unsigned 16-bit integers, doubles
 * each element, and returns the matrix
 *
 * This is a MEX-file for MATLAB.
 * Copyright 1984-2006 The MathWorks, Inc.
 */

/* $Revision: 1.9.4.6 $ */

#define NDIMS 2
#define TOTAL_ELEMENTS 4

/* the computational subroutine */
void dbl_elem(unsigned short *x)
{
  unsigned short scalar=2;
  int i,j;

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

/* the gataway function */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
  const mwSize dims[]={2,2};
  unsigned char *start_of_pr;
  unsigned short data[]={1,2,3,4};
  size_t bytes_to_copy;

  (void) nlhs; (void) nrhs; (void) prhs;  /* unused parameters */

  /* call the computational subroutine */
  dbl_elem(data);

  /* create a 2-by-2 array of unsigned 16-bit integers */
  plhs[0] = mxCreateNumericArray(NDIMS,dims,mxUINT16_CLASS,mxREAL);

  /* populate the real part of the created array */
  start_of_pr = (unsigned char *)mxGetData(plhs[0]);
  bytes_to_copy = TOTAL_ELEMENTS * (size_t)mxGetElementSize(plhs[0]);
  memcpy(start_of_pr,data,bytes_to_copy);
}

⌨️ 快捷键说明

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