A.2.4带有Mex文件的元胞数组

可以对mex文件中的Cell数组执行与inoct文件完全相同的操作。一个复制的函数的示例celldemo.ccmex文件中的oct文件从mycell.c如下所示。

#include "mex.h"

void
mexFunction (int nlhs, mxArray *plhs[],
             int nrhs, const mxArray *prhs[])
{
  mwSize n;
  mwIndex i;

  if (nrhs != 1 || ! mxIsCell (prhs[0]))
    mexErrMsgTxt ("ARG1 must be a cell");

  n = mxGetNumberOfElements (prhs[0]);
  n = (n > nlhs ? nlhs : n);

  for (i = 0; i < n; i++)
    plhs[i] = mxDuplicateArray (mxGetCell (prhs[0], i));
}

输出也与oct文件的版本相同。

[b1, b2, b3] = mycell ({1, [1, 2], "test"})
⇒
b1 =  1
b2 =

   1   2

b3 = test

请注意在示例中使用mxDuplicateArray作用这需要作为mxArray返回的指针mxGetCell可能会被困。的反函数mxGetCell,用于设置Cellvalues,是mxSetCell定义为

void mxSetCell (mxArray *ptr, int idx, mxArray *val);

最后,要创建元胞数组或矩阵,适当的函数是

mxArray *mxCreateCellArray (int ndims, const int *dims);
mxArray *mxCreateCellMatrix (int m, int n);

版权所有 © 2024-2025 Octave中文网

ICP备案/许可证号:黑ICP备2024030411号-2