JohnnyF
JohnnyF

Reputation: 1101

how to use mxNumericArray

My goal is to return a UINT32 number:

I have been using mexCreateDoubleScalar and I would like to change it to mxCreateNumericArray

The old code is

const int ALL_GOOD   0;
plhs[0]=mexCreateDoubleScalar(ALL_GOOD);

The new code i want

const int ALL_GOOD   0;
int dim[2];
dim[0]=dim[1]=1;
plhs[0]=mxCreateNumericArray(2,dims,mxUINT32_CLASS,mxREAL);

How can I put the value ALL_GOOD in plhs[0],
Or any batter way to return UINT32 from mex?

Upvotes: 0

Views: 341

Answers (1)

Shai
Shai

Reputation: 114786

You can use mxCreateNumericMatrix which is slightly more simple than the general mxCreateNumericArray to create the container mxArray, then use mxGetData to get a pointer to the actual memory of the allocated scalar and assign the value to it.

const int ALL_GOOD = 0; // need assignment here...
plhs[0] = mxCreateNumericMatrix( 1, 1, mxUINT32_CLASS,mxREAL); // create the scalar
unsigned int* ptr = (unsigned int*)mxGetData( plhs[0] );
ptr[0] = ALL_GOOD; // assign the value to plhs[0]    

Upvotes: 1

Related Questions