MATLAB: How to address a 3D array in a mex file

MATLABmex

I want to work with 3D array in a mex function but my code doesn't compile (error C2109)
It is something like this:
#include "matrix.h"
#include "mex.h"
#define phi prhs[0]
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int nDimNum;
const int *pDims;
double *pD;
mxArray *Data;
nDimNum = mxGetNumberOfDimensions(phi);
pDims = mxGetDimensions(phi);
Data = mxCreateNumericArray(nDimNum, pDims, mxDOUBLE_CLASS, mxReal);
pD = (double *) mxGetPr(Data);
for(i=i; i<pDims[0]; i++)
{ for(j=0; j<pDims[1]; j++)
{ for(k=0; k<pDims[2]; k++)
{ pD[i][j][k]=i*100+j*10+k;}
}
}
...
}
The compiling error C2109 says in the line with pD[i][j]… that the index requires an array or a pointer type.
Is there a way to work with a three-dimensional mxArray in that manner, that means, accessing single cells with this (or similar) notation?

Best Answer

This question concerns the proper usage of C, but has only a weak connection to Matlab.
If you use three indices for pD, then this must be defined as "double *pD". But I assume, you want:
mwSize s1, s2, s3;
mxArray *Data;
nDimNum = mxGetNumberOfDimensions(phi);
pDims = mxGetDimensions(phi);
Data = mxCreateNumericArray(nDimNum, pDims, mxDOUBLE_CLASS, mxReal);
pD = (double *) mxGetPr(Data);
s1 = pDims[0];
s2 = pDims[1];
s3 = pDims[2];
for(i=0; i<s1; i++) // NOT i=i !
{ for(j=0; j<s2; j++)
{ for(k=0; k<s3; k++)
{ pD[i + j * s1 + k * (s1 * s2)] = i*100+j*10+k;}
}
}
...