If you want to operate on a calling workspace variable in-place iteratively, you will have to pass that variable into the mex routine each iteration. In other words, you need to create this variable at your m-file level and pass it into your mex routine, NOT create this variable as a plhs[0]. E.g., if you pass it in as the first argument, then you will be using img0 = mxGetPr(prhs[0]) each time inside the mex routine. There will be no explicit output at the m-file level, nor should you create any plhs[0] inside the mex routine. E.g., the m-file calling syntax would be:
Allocate img0 once, then inside your loop do this:
Pdts(img0,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);
and inside the mex routine Pdts you would be modifying prhs[0] in-place each time.
CAUTION: This in-place modification violates the strict const assumption of the prhs inputs. If img0 is shared with any other variable at the m-file level you will inadvertantly modify that other variable as well and possibly get incorrect results from your code.
Alternatively to operating on the variable in-place, you can duplicate it each time if you are willing to accept the extra data copying involved. E.g.,
Allocate img0 once, then inside your loop do this:
img0 = Pdts(img0,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);
And then inside the mex routine have this statement to create the plhs[0]:
plhs[0] = mxDuplicateArray(prhs[0]);
img0 = mxGetPr(plhs[0]);
As a side note, you should be using mwSize for dims, not int. E.g.,
mwSize dims[3] = {Tnpx_h,Tnpx_w,nslc};
Best Answer