Using native vectors in matlab mex files

I am trying to use the Eigen library for C ++, which I want to make into a mex file in Matlab. I broke the code below its essence. I need to make a return vector, the length of which corresponds to the number of rows from the input matrix.

With the code below, I get the following compilation errors corresponding to the line:

double y_OUT[nrow] = {};

  • error C2057: expected constant expression error
  • error C2466: cannot allocate array of constant size 0

I cannot understand why I cannot select the length vector. By uncommenting a specific line and typing nrow, I checked that it actually contains the correct number. Can anyone give any pointers (pun intended)?

     void mexFunction(
             int          nlhs,
             mxArray      *plhs[],
             int          nrhs,
             const mxArray *prhs[]
             )
    {



      double *x_IN;
      int nrow,ncols;

      /* Check for proper number of arguments */
      //...

      x_IN = mxGetPr(prhs[0]);
      nrow = (int)mxGetM(prhs[0]);
      ncols = (int)mxGetN(prhs[0]);

      double y_OUT[nrow] = {};

      MatrixXd x=Map<MatrixXd>(x_IN,nrow,ncols);
      VectorXd Respons=VectorXd::Zero(nrow);

      Map<VectorXd>(y_OUT,nrow)=Respons.array();
      return;
    }
+3
source share
1 answer

. double y_OUT[nrow] = {}; . .

  • nrow constexpr. .
  • , 0, .

: double* y_OUT = new double[nrow];. , , . , double y_OUT[nrow], , , .

Sidenotes: , , , . IMO =)

+6

All Articles