MATLAB: Same Same But Different? mxGetPr and mxGetData…

mxcreatedoublematrixmxcreatenumericmatrixmxgetdatamxgetpr

What is the difference between mxGetPr and mxGetData ? From what I understand, they both assign a pointer to the first real data of prhs(i).
Somewhat related to that question, what is then the difference between mxCreateDoubleMatrix and mxCreateNumericMatrix/Array ?

Best Answer

mxGetData returns a (void *) type to the address of the data memory block. It can be used with a cast to get a pointer to whatever data type is in the data memory.
mxGetPr is equivalent to (double *)mxGetData. That is, it returns the same address ad mxGetData but cast as a pointer to double. E.G.,
mxArray *mx, *my;
double *pr, *qr;
int *sr;
mx = mxCreateDoubleMatrix(2,3,mxREAL);
pr = mxGetPr(mx); // pointer to first double element of mx
qr = (double *)mxGetData(mx); // pointer to first double element of mx
my = mxCreateNumericMatrix(2,3,mxINT32_CLASS,mxREAL);
sr = (int *)mxGetData(my); // pointer to first int element of my
pr and qr are essentially the same. Each one points to the first double element of mx. sr is different, it points to the first int element of my.
mxCreateDoubleMatrix creates 2D matrices where the data block contains double type data. mxCreateNumericMatrix creates 2D matrices where the data block contains a user selectable data type. mxCreateNumericArray creates nD matrices where the data block contains a user selectable data type.
mxArray *mx, *my, *mz;
mwSize ndim = 3;
mwSize dims[] = {3,4,5};
mx = mxCreateDoubleMatrix(2,3,mxREAL); // 2x3 mxArray of doubles
my = mxCreateNumericMatrix(2,3,mxINT32_CLASS,mxREAL); // 2x3 mxArray of int
mz = mxCreateNumericMatrix(ndim,dims,mxINT32_CLASS,mxREAL); // 3x4x5 mxArray of int