Same Same But Different? mxGetPr and mxGetData...

10 views (last 30 days)
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 ?

Accepted Answer

James Tursa
James Tursa on 5 Jun 2014
Edited: James Tursa on 5 Jun 2014
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
  4 Comments
Zhong Hao
Zhong Hao on 9 Jun 2014
Hi James, I do not get the part where you said "an integer that holds the address of the first element of the data block"
My interpretation of that statement is "the address of the first element of the prhs is stored as an integer type instead of a pointer type". To dereference the value, it is necessary to use the %val.
Is this what you mean?
James Tursa
James Tursa on 9 Jun 2014
Edited: James Tursa on 9 Jun 2014
An mxArray variable consists of a header structure that contains many things such as: Class (double, single, int32, etc), dimensions, real data pointer, and imaginary data pointer. On a 32-bit system, the data pointers will be 32-bit words storing the address of the data blocks (where the double etc data is actually stored in memory). The mxGetPr and mxGetData functions in the C API actually return a pointer type. The mxGetPr and mxGetData functions in the Fortran API do not, they simply return the value of the real data address in a 32-bit integer. That's all I am trying to say. Specifically, it is NOT a Fortran pointer (which is typically a structure in and of itself containing address & dimensions etc, at least for non-scalar pointers).

Sign in to comment.

More Answers (0)

Categories

Find more on Fortran with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!