MATLAB: MEX problem with mxGetData

mex

Hi everyone !
I need advice to get this type of arguments:
  • prhs[1] = {2, [5:12]}
  • prhs[1] = {3, [ ], [1:5]}
  • prhs[1] = {{4,6},[1:5], {1,8,9}, [3:6]} .
When I run the function I can find the good array with plhs[2] but I can't find the way to extract data, for further processing!
I know I have to use a pointer like mxGetData, but I'm trying every way except the good one I think !
if(nrhs==2)
{
mxArray * extraits= mxDuplicateArray(prhs[1]);
int n = mxGetN(extraits);
double * a= (double*)mxGetData(extraits);
/* Here, how can I find 2, and 5:12 for example ??? */
plhs[2]=extraits;
}
Thanks a lot !!!!

Best Answer

When you use the curly braces { } in MATLAB you are building a cell array, so you need to use mxGetCell in your mex routine. E.g.,
prhs[1] = {2, [5:12]}
prhs[1] = {3, [ ], [1:5]}
prhs[1] = {{4,6},[1:5], {1,8,9}, [3:6]}
In all of these cases you can get at the data as follows:
mxArray *cell;
double *pr;
mwSize i, j, n, ncell;
:
if( mxIsCell(prhs[1]) ) {
ncell = mxGetNumberOfElements(prhs[1]);
for( i=0; i<ncell; i++ ) {
cell = mxGetCell(prhs[1],i);
if( mxIsEmpty(cell) ) {
// Code to handle empty case
} else {
if( mxIsDouble(cell) {
n = mxGetNumberOfElements(cell);
pr = mxGetPr(cell);
for( j=0; j<n; n++ ) {
// Code to manipulate pr[j] here
}
} else {
// Code to handle non-double case
}
}
}
}