Ho to extract an C array from Mex function (pointer to pointer to pointer ; double ***)

1 view (last 30 days)
Dear Community,
I've got this problem where I need to interface an old C-algorithm into my new Matlab program.
This C-function needs as one input a double*** array. I allacoate some memory inside the mexFunction via:
double ***rv_solve;
rv_solve = (double ***)mxCalloc(X_SIZE, sizeof(double *));
for (i = 0 ; i < X_SIZE; i++) {
rv_solve[i] = (double **)mxCalloc(Y_SIZE, sizeof(double *));
for (j = 0; j < Y_SIZE; j++)
rv_solve[i][j] = (double *)mxCalloc(Z_SIZE, sizeof(double *));
}
The C-function manipulates this 3D array and I want this array back into my Matlab workspace.
The output gets assigned by
rv_solve_out = (double *)mxGetPr(plhs[2]);
How do I now associate double*** rv_solve with double* rv_solve_out ?
All I found on the web and the documantation is about 1-Dim arrays. I would love to use just 1-Dim arrays and use superindexing, but I need this C-function and it is complicated enough not to rewrite it for 1-Dim inputs.
And probably there is an easy solution, as most times, but I'm just to stupid to find or recognize it.
Thanks in advance.
Cheers Achim

Accepted Answer

Achim
Achim on 28 Aug 2014
I found a workaround, which was to obvious for me... Just transform the 3D array into a 1D array via superindexing.
for (i = 0 ; i < dims[0] ; i++) {
for (j = 0 ; j < dims[1] ; j++) {
for (k = 0 ; k < dims[2] ; k++) {
rv_solve_out[k*dims[0]*dims[1] + j*dims[1] + i]=rv_solve[i][j][k];
}
}
}

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!