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

📄 phonebook.c

📁 matlab宝典,电子工业出版社,包含该书的源码12-14章
💻 C
字号:
#include "mex.h"
#include "string.h"

#define MAXCHARS 80   /* max length of string contained in each 
                         field */

/* The gateway routine.  */
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
  const char **fnames;       /* pointers to field names */
  const int  *dims;
  mxArray    *tmp, *fout;
  char       *pdata;
  int        ifield, jstruct, *classIDflags;
  int        NStructElems, nfields, ndim;
    
  /* Check proper input and output */
  if (nrhs != 1)
    mexErrMsgTxt("One input required.");
  else if (nlhs > 1)
    mexErrMsgTxt("Too many output arguments.");
  else if (!mxIsStruct(prhs[0]))
    mexErrMsgTxt("Input must be a structure.");

  /* Get input arguments */
  nfields = mxGetNumberOfFields(prhs[0]);
  NStructElems = mxGetNumberOfElements(prhs[0]);

  /* Allocate memory  for storing classIDflags */
  classIDflags = mxCalloc(nfields, sizeof(int));

  /* Check empty field, proper data type, and data type
      consistency; get classID for each field. */
  for (ifield = 0; ifield < nfields; ifield++) {
    for (jstruct = 0; jstruct < NStructElems; jstruct++) {
      tmp = mxGetFieldByNumber(prhs[0], jstruct, ifield);
      if (tmp == NULL) {
        mexPrintf("%s%d\t%s%d\n",
            "FIELD:", ifield+1, "STRUCT INDEX :", jstruct+1);
        mexErrMsgTxt("Above field is empty!"); 
      } 
      if (jstruct == 0) {
        if ((!mxIsChar(tmp) && !mxIsNumeric(tmp)) || 
            mxIsSparse(tmp)) { 
          mexPrintf("%s%d\t%s%d\n", 
              "FIELD:", ifield+1, "STRUCT INDEX :", jstruct+1);
          mexErrMsgTxt("Above field must have either "
              "string or numeric non-sparse data.");
        }
        classIDflags[ifield] = mxGetClassID(tmp); 
      } else {
        if (mxGetClassID(tmp) != classIDflags[ifield]) {
          mexPrintf("%s%d\t%s%d\n", 
              "FIELD:", ifield+1, "STRUCT INDEX :", jstruct+1);
          mexErrMsgTxt("Inconsistent data type in above field!");
        } 
        else if (!mxIsChar(tmp) && ((mxIsComplex(tmp) || 
            mxGetNumberOfElements(tmp) != 1))) {
          mexPrintf("%s%d\t%s%d\n", 
              "FIELD:", ifield+1, "STRUCT INDEX :", jstruct+1);
          mexErrMsgTxt("Numeric data in above field "
              "must be scalar and noncomplex!");
        }
      }
    }
  }

  /* Allocate memory  for storing pointers */
  fnames = mxCalloc(nfields, sizeof(*fnames));

  /* Get field name pointers */
  for (ifield = 0; ifield < nfields; ifield++) {
    fnames[ifield] = mxGetFieldNameByNumber(prhs[0],ifield);
  }

  /* Create a 1x1 struct matrix for output */
  plhs[0] = mxCreateStructMatrix(1, 1, nfields, fnames);
  mxFree(fnames);
  ndim = mxGetNumberOfDimensions(prhs[0]);
  dims = mxGetDimensions(prhs[0]);
  for (ifield = 0; ifield < nfields; ifield++) {
    /* Create cell/numeric array */
    if (classIDflags[ifield] == mxCHAR_CLASS) {
      fout = mxCreateCellArray(ndim, dims);
    } else {
      fout = mxCreateNumericArray(ndim, dims, 
          classIDflags[ifield], mxREAL);
      pdata = mxGetData(fout);
    }

    /* Copy data from input structure array */
    for (jstruct = 0; jstruct < NStructElems; jstruct++) {
      tmp = mxGetFieldByNumber(prhs[0],jstruct,ifield);
      if (mxIsChar(tmp)) {
        mxSetCell(fout, jstruct, mxDuplicateArray(tmp));
      } else {
        size_t     sizebuf;
        sizebuf = mxGetElementSize(tmp);
        memcpy(pdata, mxGetData(tmp), sizebuf);
        pdata += sizebuf;
      }
    }

    /* Set each field in output structure */
    mxSetFieldByNumber(plhs[0], 0, ifield, fout);
  }
  mxFree(classIDflags);
  return;
}

⌨️ 快捷键说明

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