Same Same But Different? mxGetPr and mxGetData...

15 次查看(过去 30 天)
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 ?

采纳的回答

James Tursa
James Tursa 2014-6-5
编辑:James Tursa 2014-6-5
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 个评论
Zhong Hao
Zhong Hao 2014-6-9
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 2014-6-9
编辑:James Tursa 2014-6-9
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).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write Fortran Programs to Read MAT File Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by