Same Same But Different? mxGetPr and mxGetData...

조회 수: 15 (최근 30일)
Zhong Hao
Zhong Hao 2014년 6월 5일
편집: James Tursa 2014년 6월 9일
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