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 09:18:02 +0000 (UTC)
Organization: Boeing
Lines: 60
Message-ID: <h75j0a$rg4$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>
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 1251364682 28164 172.30.248.38 (27 Aug 2009 09:18:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 27 Aug 2009 09:18:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:566366


"Ramana murthy" <omurthy@yahoo.com> wrote in message <h75fld$ort$1@fred.mathworks.com>...
> Hi James,
> As I have posted in my previous mail,
> My matlab code calling this mex file is 
> 
> *********************************
> clear all
> clc
> img = imread('cameraman.tif');
> for i=1:3 
>     img1 = im2double(imresize(img,0.01*i));
>     sources{i} = img1(:,:,1)';
> end
> struct1(sources)
> **************
> Clearly, I'm passing a matrix of double values.

OK, please clear something up. Is your function called "struct1", and are you passing in your "sources" variable? If that is the case, then you are passing in a cell array, not a double. e.g., try out this code to verify what the mex file is actually getting:

#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++){
        if( !mxIsDouble(prhs[i]) ) {
            mexPrintf("Input argument is not double, it is %s\n",mxGetClassName(prhs[i]));
            mxDestroyArray(cell_array_ptr);
            mexErrMsgTxt("Returning to MATLAB");
        }
        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
}

After doing that, please advise me what, exactly, you would like the mex file to do, as I have been under the apparently mistaken impression you wanted to copy some double inputs into a cell array for manipulation inside the mex routine.

James Tursa