MATLAB Answers


Problem using mxGetPr for accessing array of type double

Asked by AP
on 21 Oct 2012

Dear All,

I am using mxGetPr function to retrieve the data of an array of type double.

#include "matrix.h"
#include "math.h"
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    #define FACES prhs[0]
      int nFaces;
      double *faces;
      nFaces = mxGetM( FACES );                  
      faces = mxGetPr( FACES );        
      for( int i=0; i<nFaces ; i++ )
          mexPrintf("(%d): [ %d  ]\n", i, faces[i] );

When I print the value of the first column of prhs[0], it's all zero while those of the input array is not zero and has different values. I checked with the documentation and many examples onlines and didn't see any difference between my code and theirs.

Could someone help me how to access the value of the elements of prhs[0] correctly? I also checked the type of input array using whos command and it is double.



  1 Comment

on 21 Oct 2012

I changed it to mwSize but still I don't have access to correct values of prhs[0].



No products are associated with this question.

1 Answer

Answer by Jan Simon
on 21 Oct 2012
 Accepted answer

The "%d" format in mexPrintf does not match the type double. Better:

mexPrintf("(%d): [ %f  ]\n", i, faces[i] );

  1 Comment

on 21 Oct 2012

Great catch! Thanks.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!