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: Thu, 27 Aug 2009 15:37:01 +0000 (UTC)
Organization: Boeing
Lines: 80
Message-ID: <h7696t$i7u$1@fred.mathworks.com>
References: <h6tl9j$a0p$1@fred.mathworks.com> <h6tp1k$sja$1@fred.mathworks.com> <h72u3f$5qi$1@fred.mathworks.com> <h73knq$o06$1@fred.mathworks.com> <h74ttb$t3r$1@fred.mathworks.com> <h75bq8$jk6$1@fred.mathworks.com> <h75fld$ort$1@fred.mathworks.com> <h75j0a$rg4$1@fred.mathworks.com> <h75kdb$8c6$1@fred.mathworks.com> <h75tia$ov3$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1251387421 18686 172.30.248.35 (27 Aug 2009 15:37:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 27 Aug 2009 15:37:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:566476


"Ramana murthy" <omurthy@yahoo.com> wrote in message <h75tia$ov3$1@fred.mathworks.com>...
> INput from Matlab file 
> A cell of size k, containing a MxN matrix entries
> Desired variable in C MEx file
> A 3D matrix data[i][j][k] where k is the kth cell entry
> or equivalent representation so that I can access MxNth matrix elements
> 
> with regards,
> ramana 

Try this:

#include "mex.h"
#define DOUBLEDATA(i,j) doubledata[i+j*m+k*m*n]
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    mxArray *mxcell, *mxdouble;
    mwSize i, j, m, n, k, p, numel;
    mwSize ndim = 3;
    mwSize dims[3];
    double *celldata, *doubledata;
    
    (void)plhs; /* unused parameter */

    if( nrhs != 1 ) {
        mexErrMsgTxt("Need exactly one input");
    }
    if( nlhs != 0 ) {
        mexErrMsgTxt("Too many outputs");
    }
    if( !mxIsCell(prhs[0]) ) {
        mexPrintf("Input argument is not cell, it is %s\n",mxGetClassName(prhs[0]));
        mexErrMsgTxt("Returning to MATLAB");
    }
    
    p = mxGetNumberOfElements(prhs[0]);
    dims[2] = p;
    
    /* Fill 3D matrix with input arguments */
    for( i=0; i<p; i++){
        mxcell = mxGetCell(prhs[0],i);
        if( !mxIsDouble(mxcell) ) {
            mexPrintf("Input cell is not double, it is %s\n",mxGetClassName(mxcell));
        }
        if( mxIsComplex(mxcell) ) {
            mexErrMsgTxt("Input cell cannot be complex");
        }
        if( mxGetNumberOfDimensions(mxcell) != 2 ) {
            mexErrMsgTxt("Input cell must be 2D");
        }
        if( i == 0 ) {
            m = mxGetM(mxcell);
            n = mxGetN(mxcell);
            dims[0] = m;
            dims[1] = n;
            mxdouble = mxCreateNumericArray(ndim, dims, mxDOUBLE_CLASS, mxREAL);
            numel = mxGetNumberOfElements(mxcell);
            doubledata = mxGetPr(mxdouble);
        }
        if( mxGetM(mxcell) != m || mxGetN(mxcell) != n ) {
            mexErrMsgTxt("Input cell dimensions must all be the same");
        }
        celldata = mxGetPr(mxcell);
        for( j=0; j<numel; j++ ) {
            *doubledata++ = *celldata++;
        }
    }
    doubledata = mxGetPr(mxdouble);
    for (k=0; k<p; k++) {
        for (j=0; j<n; j++) {
            for(i=0; i<m; i++) {
                mexPrintf("data(%d,%d,%d) = %g \n", i+1, j+1, k+1, DOUBLEDATA(i,j,k));
            }
        }
    }
    // other code to use mxdouble
    mxDestroyArray(mxdouble); // do this if not returned in a plhs
}

James Tursa