I've spent a week on the following problem and I can't for the life of me figure it out! I'm going to be as brief as possible with the code and chop out irrelevant lines but it should be clear as to my problem. For starters, I'm using Matlab in combination with C, which communicates via mex files. Without further ado…
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ static double *U plhs[4] = mxCreateNumericArray(2,dims,mxDOUBLE_CLASS,mxREAL); U = (double*)mxGetPr(plhs[4]); /* C code which solves for "U" based on a number of other input variables*/ solve(U,...,...,...) /* C code which solves for "U" based on a number of other input variables*/ derivative(U,...,...,...) }
After execution, everything works fine and I have the value for the derivative of "U". I then wanted to compare solvers so I'm swapping out the "solve(U)" for a Matlab function which I call via "mexCallMATLAB". Here is where I get lost
(Again I removed irrelevant variables)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ static double *U plhs[4] = mxCreateNumericArray(2,dims,mxDOUBLE_CLASS,mxREAL); U = (double*)mxGetPr(plhs[4]); /* Call MATLAB solver */ mxArray *Uin[8],*Uout[2]; Uin[0] = mxCreateNumericArray(2,dims,mxSINGLE_CLASS,mxREAL); memcpy(mxGetPr(Uin[0]),(some variable),m*n*sizeof(float));
yes there are 8 inputs…I just removed for simplicity
mexCallMATLAB(2,Uout,8,Uin,"my_matlab_solver");
I then check the results of "Uout" with the following:
mexCallMATLAB(0,NULL,1,&Uout[0],"plot_variable");
Everything works out great, but the "C" code that later calls on the variable "U" to find it's derivative does not work.
plhs[4] = Uout[0]; /* C code which solves for "U" based on a number of other input variables*/ derivative(U,...,...,...)}
I can not figure out how to assign "Uout[0]" to "U". I thought by setting plhs[4] = Uout[0] then U would point to the results from "my_matlab_solver" but it does not. There are no compile errors.
Is there easier way where I can assign the output of "my_matlab_solver" directly to "U" with out having to make a mxArray for the output? This whole MEX thing seems a lot more complicated than it needs to be. Thanks for you help!
************************************************************************** ************************************************************************** **************************************************************************
EDIT: I'm going to be very blunt
I have a variable called "U" which is defined as follows
U = (double*)mxGetPr(plhs[4]);
How do I assign the output from this mexCallMATLAB
mexCallMATLAB(2,Uout,8,Uin,"my_matlab_solver");
to "U"? I tried this; plhs[4] = Uout[0]; but it does not work. Don't worry about the rest of the code it's fine. My problem is strictly related to assigning the output of mexCallMATLAB to a variable defined as "U" is above.
Best Answer