Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to pass cell of 3D matrices to C mex
Date: Wed, 26 Aug 2009 15:35:22 +0000 (UTC)
Organization: Boeing
Lines: 39
Message-ID: <h73knq$o06$1@fred.mathworks.com>
References: <h6tl9j$a0p$1@fred.mathworks.com> <h6tp1k$sja$1@fred.mathworks.com> <h72u3f$5qi$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1251300922 24582 172.30.248.38 (26 Aug 2009 15:35:22 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 26 Aug 2009 15:35:22 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:566161


"oruganti murthy" <omurthy@yahoo.com> wrote in message <h72u3f$5qi$1@fred.mathworks.com>...
>  

Try this:

#include "mex.h"
#define DATA(i,j) data[i+j*m]
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    mxArray *cell_array_ptr, *cell_element_ptr;
    mwIndex i, j, index;
    mwSize m, n;
    double *data;
    
    (void)plhs; /* unused parameter */
    
    /* Create a nrhs x 1 cell mxArray. */
    cell_array_ptr = mxCreateCellMatrix(nrhs, 1);
    
    /* Fill cell matrix with input arguments */
    for( i=0; i<nrhs; i++){
        mxSetCell(cell_array_ptr, i, mxDuplicateArray(prhs[i]));
    }
    for (index=0; index<nrhs; index++) {
        cell_element_ptr = mxGetCell(cell_array_ptr, index);
        data = mxGetPr(cell_element_ptr);
        m = mxGetM(cell_element_ptr);
        n = mxGetN(cell_element_ptr);
        mexPrintf("Cell array index %d\n",index);
        for (i=0; i<m; i++)
            for(j=0; j<n; j++)
                mexPrintf("data(%i,%i) = %g \n", i+1, j+1, DATA(i,j));
        // other code to use cell_element_ptr
    }
    // other code to use cell_array_ptr
    mxDestroyArray(cell_array_ptr); // so this if not returned in a plhs
}

James Tursa