I am trying to figure out how to access a matrix that is stored in a field in a matlab structure from a mex function.
This is an awfully long time ... Let me explain:
I have a matlab structure that has been defined as follows:
matrixStruct = struct('matrix', {4, 4, 4; 5, 5, 5; 6, 6 ,6})
I have a mex function in which I would like to get a pointer to the first element in the matrix (matrix [0] [0], in c terms), but I could not figure out how to do this.
I tried the following:
double *ptr = mxGetPr(mxGetField(prhs[0], 0, "matrix"); for(i = 0; i < 3; i++){ printf("%f\n", *(ptr + (i * 3))); printf("%f\n", *(ptr + 1 + (i * 3))); printf("%f\n", *(ptr + 2 + (i * 3))); }
As a result of this, the following occurs:
4.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
I also tried options for the following, thinking that maybe it was something warlike with nested function calls, but to no avail:
mxArray *fieldValuePtr = mxGetField(prhs[0], 0, "matrix"); double *ptr = mxGetPr(fieldValuePtr);
Does anyone have an idea on how I can achieve what I'm trying to do, or what I can do wrong?
Thanks!
Edit: according to yuk's comment, I tried to perform similar operations on a structure that has a field called an array, which is a one-dimensional doubling array.
The structure containing the array is defined as follows:
arrayStruct = struct('array', {4.44, 5.55, 6.66})
I tried the following in arrayStruct from the mex function:
mptr = mxGetPr(mxGetField(prhs[0], 0, "array")); printf("%f\n", *(mptr)); printf("%f\n", *(mptr + 1)); printf("%f\n", *(mptr + 2));
... but the result followed what was printed earlier:
4.440000 0.000000 0.000000