首页 文章

在mex文件的输出处的int数组

提问于
浏览
4

我正在尝试创建一个mex函数,其条目是一个整数,其输出是一个整数数组 . 所以函数看起来像:int * myFunction(unsigned int N) . 在mexFunction中,我声明了一个int类型的变量* variab然后

N = mxGetScalar(prhs[0]);

  /* assign a pointer to the output */
  siz= 2*ceil(log(1.0*N)/log(2.0)-0.5)+1;
  plhs[0] = mxCreateDoubleMatrix(1,siz, mxREAL);
  vari = (int*) mxGetPr(plhs[0]); */
  /* Call the subroutine. */
  vari = myFunction(N);
  mexPrintf("The first value is %d\n", vari[0]);

事情是第一个值是正确的(并且其他值被检查并且也是正确的)但是当我调用例程mxFunction(16)时,我只获得0作为输出 . 我想这是因为我的输出是一个int数组但我不知道如何解决这个问题 . 任何提示?干杯 .

1 回答

  • 3

    Matlab默认处理双打 . 您可以根据代码片段轻松地将它们转换为mex函数,如下例所示 . 我创建了一个执行演示算法的myFunction . 我没有返回数据类型,而是将其设为void函数,并将指针传递给输出,以便它可以填充它 . . .

    /*************************************************************************/
    /* Header(s)                                                             */
    /*************************************************************************/
    #include "mex.h"
    #include "math.h"
    
    
    /*************************************************************************/
    /*the fabled myFunction                                                  */
    /*************************************************************************/
    void myFunction(unsigned int N, unsigned int siz, double* output)
    {
        int sign = 1;
        for(int ii=0; ii<siz; ++ii)
        {
            output[ii] = (double)(ii * sign + N);
            sign *= -1;
        }
    
    }
    
    
    /*************************************************************************/
    /* Gateway function and error checking                                   */
    /*************************************************************************/
    void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    {
      /* variable declarations */
      unsigned int siz;
      double N;
    
      /*  check the number of input and output parameters  */  
        if(nrhs!=1)
            mexErrMsgTxt("One input arg expected");
        if(nlhs > 1)
            mexErrMsgTxt("Too many outputs");
    
      N = mxGetScalar(prhs[0]);
    
      /* assign a pointer to the output */  
      siz= 2*ceil(log(1.0*N)/log(2.0)-0.5)+1;
      plhs[0] = mxCreateDoubleMatrix(1,siz, mxREAL);
      myFunction(N, siz, mxGetPr( plhs[0]) );
    }
    

相关问题