MEX: Problem creating a cell matrix and assign it to the output
Mostrar comentarios más antiguos
Dear All,
By using a MEX file, I am trying to loop through array "group_ptr" with size 1000×1 to make a cell with size 4×1. group_ptr(i) contains the group ID which "i" belongs to. The 4 groups have different number of elements. My MEX file does other things beside to creating this cell. It takes one input and generates 4 outputs. When it gets to making the cell, MATLAB crashes.
However, when I comment the part for making the cell and reduce the number of outputs to 3, MEX file works fine. I would be grateful if someone could help me what the problem could be.
Below is the part of my code which creates this cell. The MEX file has 4 output which the last one is this cell.
#define CELLGROUP plhs[3]
mxArray *CellArray_ptr;
CellArray_ptr = mxCreateCellMatrix( 4, 1 );
double *a = new double[ 1000 ];
for( mwIndex i=0; i<4; i++ )
{
int counter = 0;
for( int j=0; j < 1000; j++ )
{
if( (mwIndex)group_ptr[ j ]== (i+1) )
a[ counter++ ] = j;
}
mxArray *A;
A = mxCreateDoubleMatrix( counter, 1, mxREAL );
memcpy( mxGetPr(A), a, sizeof(double)*counter );
mxSetCell( CellArray_ptr, i, A );
mxDestroyArray( A );
}
CELLGROUP = CellArray_ptr;
delete[] a;
Thanks,
Ahmad
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!