Mex, how to copy an array to output
4 views (last 30 days)
In my mex function my_expm.c, I calculated matrix expoential for the input matrix,
void mexFunction(int nlhs,mxArray *plhs, int nrhs, const mxArray *prhs)
inputMat = mxGetDoubles(prhs);
numRows = mxGetM(prhs);
plhs = mxCreateDoubleMatrix(numRows, numRows, mxREAL);
outputMat = mxGetDoubles(plhs);
outputMat = r8mat_expm1(numRows, inputMat); // matrix exponential
Calling it by
c = my_expm([1,0;0,1])
will return all zeros, as explained in this post.
I tried the two suggestions in that post to fix this problem, both failed.
r8mat_expm1(numRows, inputMat, outputMat)
outputMat = r8mat_expm1(numRows, inputMat);
this still returns all zeros. I used gdb to debug, it turns out when using outputMat = r8mat_expm1(numRows, inputMat)，the outputMat in mex is right and when it returns to matlab, they all become zero. But when using r8mat_expm1(numRows, inputMat, outputMat), the outputMat in mex are already all zeros. I do not know why they are zeros since I only simply changed the signature of r8mat_expm1().
by using mxSetDoubles(), as in this post. This call get the work done. However, this mex function can run only once, if I call it again, matlab will crash. I can not see whether there is a problem with mxSetDoubles().
So, is there another way that I can simply copy outputMat to plhs?