How do I read an input array of type double in mex?

I am creating an Array of type double using arr = rand(5,1) I simply want to be able to pass into into my mex function and read the values
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *A;
const mwSize *dims;
//dims = mxGetDimensions(prhs[0]);
//M = (int)dims[0]; N = (int)dims[1];
A = mxGetData(prhs[0]);
mexPrintf("%d\n", A[0]);

Answers (1)

You almost had it correctly. But the %d format is for integer types only, not floating point. So use one of the floating point formats, e.g. %f or %e or %g.
mexPrintf("%f\n", A[0]);

Categories

Tags

Asked:

on 27 Jun 2016

Edited:

on 27 Jun 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!