Wednesday, October 22, 2008

Matlab: passing Cell into ansi C mex file

Let the test cell defined in matlab betest_cell={1,[2,3],[5,6,7;8,9,10;11,12,13]};

To get these values in mex C file we can write in functionvoid mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] ) {

const mxArray* temp_cell;
mxArray* cells[3];

temp_cell=prhs[0]; //cell is the only variable
//passed to mex file.
cells[0]=mxGetCell(temp_cell,0);
cells[1]=mxGetCell(temp_cell,1);
cells[2]=mxGetCell(temp_cell,2);

mexPrintf("%f,", *mxGetPr(cells[0]));
mexPrintf("\n{%f,%f}", *mxGetPr(cells[1]),
*(mxGetPr(cells[1])+1));

int N;
N= mxGetM(cells[2]);
double **Img = makeMatrixFromVector(mxGetPr(cells[2]),N);


int i,j;
for (i=0;i<N;i++) {
mexPrintf("\n");
for (j=0;j<N;j++) {
mexPrintf("\t%.1f ",Img[i][j]);
}
}
mexPrintf("\n")


}

This should give in matlab console:1.000000,
[2.000000 3.000000]
5.0 6.0 7.0
8.0 9.0 10.0
11.0 12.0 13.0

makeMatrixFromVector is defined as follows:double ** makeMatrixFromVector(double *inData,int size) {
int x,y;
double ** Img = (double**)mxMalloc(size*sizeof(double*));
for (y=0 ; y< size ; y++) {
Img[y]=(double*) mxMalloc((size)* sizeof(double));
for (x=0 ; x< size ; x++) {
Img[y][x]=*(inData+x*size+y);
}
}
return Img;
}