Hello!
I have written a MEX file that takes in a cell array of double matrices, each of dimension 250×1000. The cell array dimensions are 29×1. The MEX file is assigning the value 3 to all the entries of the matrices. However, whenever I call the MEX file, MATLAB crashes. I have no idea why this is happening as the index of my matrices are in the allowed range.
To reproduce the bug/error, please run the following code on the attached files:
load test; test(pp);
For a quick review, the test.c file is as follows:
#include <math.h>#include <matrix.h>#include <mex.h>#include <stdio.h>#include <string.h>#include <stdlib.h>void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ const mwSize *dims; const mxArray *cell; const mxArray *cellArray; double *inMatrix; int mom, cellSize, jcell; mwIndex i, j, count; mwSize ncol, nrow; // Read the cell cell = prhs[0]; dims = mxGetDimensions(prhs[0]); for (jcell=1; jcell<dims[0]; jcell++) { cellArray = mxGetCell(cell,jcell); inMatrix = mxGetPr(cellArray); nrow = mxGetM(cellArray); ncol = mxGetN(cellArray); printf("%d, %d, %d\n", nrow, ncol, cellSize); count = 0; for(i=0;i<nrow;i++){ for(j=0;j<ncol;j++){ inMatrix[count] = 3; count++; } } } }
Thanks
Best Answer