| MATLAB Application Program Interface | Search  Help Desk |
| mxGetPr | Examples See Also |
Get an mxArray's real data elements
#include "matrix.h" double *mxGetPr(const mxArray *array_ptr);array_ptr
mxArray.
The address of the first element of the real data. Returns NULL if there is no real data.
Call mxGetPr to determine the starting address of the real data in the mxArray that array_ptr points to. Once you have the starting address, it is fairly easy to access any other element in the mxArray.
Consider a MEX-file named DispReal that displays the value of every real element in the input mxArray
#include "mex.h"
void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray
*prhs[])
{
int c, total_num_of_elements;
double *real_data_ptr;
if (mxIsDouble(prhs[0])) {
/* Get starting address of real data in input array. */
real_data_ptr = (double *)mxGetPr(prhs[0]);
/* Using pointer auto-increment, display every element in
the array. */
total_num_of_elements = mxGetM(prhs[0]) * mxGetN(prhs[0]);
/* Display the contents of every real value. */
for (c = 0; c < total_num_of_elements; c++)
mexPrintf("%g\n", *real_data_ptr++);
}
else
mexErrMsgTxt("First argument must be a double array.");
}
In MATLAB, create Values:
>> Values = [2 3; 5 7] Values = 2 3 5 7Pass
Values as an argument to DispReal
>> DispReal(Values) 2 5 3 7For additional examples, see
mxgetpr.c and mxgetpr2.c in the mx subdirectory of the examples directory.
mxGetPi, mxSetPi, mxSetPr