Hello,
I am trying to create a mex file which will modify the contents of the array in plhs, but whenever I run the code it crashes when I try to assign the adress of a double* to plhs[0]. Below is my code…
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int l = (int)mxGetScalar(prhs[0]); int nslc = (int)mxGetScalar(prhs[1]); double *spc = (double*)mxGetData(prhs[2]); int Tnpx_w = (int)mxGetScalar(prhs[3]); int Tnpx_h = (int)mxGetScalar(prhs[4]); double *Tpx_w0 = (double*)mxGetData(prhs[5]); double *Tpx_h0 = (double*)mxGetData(prhs[6]); int Inpx_w = (int)mxGetScalar(prhs[7]); int Inpx_h = (int)mxGetScalar(prhs[8]); double *Ipx_w = (double*)mxGetData(prhs[9]); double *Ipx_h = (double*)mxGetData(prhs[10]); double *a = (double*)mxGetData(prhs[11]); double *D = (double*)mxGetData(prhs[12]); double alpha = (double)mxGetScalar(prhs[13]); double OMEGA = (double)mxGetScalar(prhs[14]); double *W = (double*)mxGetData(prhs[15]); double *img = (double*)mxGetData(prhs[16]); int dims[3] = {Tnpx_h,Tnpx_w,nslc}; int s,m,n; double L, Tpx_w, Tpx_h; double TposX, TposY, TposZ; double SposX, SposY, SposZ; double IX, IY; int IXi, IYi; double VX, VY, VZ; double *ts_posX = (double*)mxMalloc(Tnpx_w*sizeof(double)); double *ts_posY = (double*)mxMalloc(Tnpx_h*sizeof(double)); double *img0; // plhs[0] = mxCreateNumericArray(3, dims, mxDOUBLE_CLASS, mxREAL); printf("test0"); img0 = mxGetPr(plhs[0]); printf("test1"); //begin code ... }
An example of how I call this in matlab is…
img0=Pdts(l,nslices,spacing,Tnpx_w,Tnpx_h,Tpx_w0,Tpx_h0,Inpx_w,Inpx_h,Ipx_w,Ipx_h,a,D,alpha,OMEGA,W(count),img);
this is in a large loop where img0 is a 3d array initially set to all zeros. Each call should update the array img0 to add in new values to what is in img0 when Pdts is called. The c code compiles fine, but it is crashing on the line
img0 = mxGetPr(plhs[0]);
I know this because the first printf statement will execute, but the second will not. If I add in the commented out line
plhs[0] = mxCreateNumericArray(3, dims, mxDOUBLE_CLASS, mxREAL);
it will not crash and run. However, each time I run it I reallocate a new space within the memory and so img0 does not retain the values from previous calls to Pdts. Could anyone give me some help with getting this working? Thanks
Best Answer