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 09:09:04 +0000 (UTC)
Organization: Indian Institute of Technology
Lines: 60
Message-ID: <h72u3f$5qi$1@fred.mathworks.com>
References: <h6tl9j$a0p$1@fred.mathworks.com> <h6tp1k$sja$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1251277744 5970 172.30.248.37 (26 Aug 2009 09:09:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 26 Aug 2009 09:09:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 223861
Xref: news.mathworks.com comp.soft-sys.matlab:566058


 
> Pass it in like any other variable. Then prhs[0] will be the cell array. If you want to modify it, use mxDuplicateArray to create a deep copy. Then use mxGetCell followed by mxGetPr to get at the cells and the arrays contained in the cells respectively. Why did you write that "duplicate will not work"?
> 
> James Tursa

Hi James,
I thought that duplicating will not allow the matrix to change. After your comment, I have verified it and I am taking my statement back.
As per your guidance, I wrote the following. and got the following output.
Can you please comment?

with regards,
ramana
************
#include "mex.h"
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, total_num_of_cells, total_num_of_elements;
    double **carray;
    double *data;
    
    (void)plhs;    /* unused parameter */
    
    /* Create a nrhs x 1 cell mxArray. */
    cell_array_ptr = mxCreateCellMatrix((mwSize)nrhs, 1);
    
    /* Fill cell matrix with input arguments */
    for( i=0; i<(mwIndex)nrhs; i++){
        mxSetCell(cell_array_ptr, i, mxDuplicateArray(prhs[i]));
    }
    total_num_of_cells = mxGetNumberOfElements(cell_array_ptr);
    
    for (index=0; index<total_num_of_cells; index++)  {
        cell_element_ptr = mxGetCell(cell_array_ptr, index);
        data = mxGetPr(cell_element_ptr);
        m = mxGetM(cell_element_ptr);
        n = mxGetN(cell_element_ptr);
        carray = mxCalloc(n, sizeof(double*));
        for (j=0; j<n; j++)
            carray[j] = data+m*j;
        for (i=0;i<m;i++)
            for(j = 0;j<n;j++)
                mexPrintf("data(%i,%i) = %f \n", i+1, j+1, carray[i][j]);
    }
}
*********************************
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)
**************
data(1,1) = 0.000000 
data(1,2) = 0.000000 
data(1,3) = 0.000000 
>>