MATLAB: MEX Function – Using mexCallMATLAB function in conjunction with “PolyFit”

MATLABmexcallmatlab

I am just starting to work with MEX functions, and have run into a situation that I am currently unable to solve. I am trying to use the mexGetMATLAB function with "polyfit" and an not able to pass in the correct array of pointers for the input. My call is as follows :
mexCallMATLAB(0,NULL,3,Plot_Pointer,"polyfit");
where Plot_Pointer is defined as
mxArray *Plot_Pointer[3] = {pm, pp, fit_order}; and pm, pp and fit_order are defined as mxArray *pm[1], *pp[1], *fit_order[1]
The snipit of code that sets up all the required inputs (sample data for now just to try and get it working) is as follows:
mwSize m, n;
mxArray *rhs[1], *lhs[1], *pm[1], *pp[1], *fit_order[1];
mxArray *Plot_Pointer[3] = {pm, pp, fit_order};
pm[0] = mxCreateDoubleMatrix(10, 1, mxREAL);
pp[0] = mxCreateDoubleMatrix(10, 1, mxREAL);
fit_order[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
pmp = mxGetPr(pm[0]);
ppp = mxGetPr(pp[0]);
myarray = mxGetPr(fit_order[0]);
for (i=0; i<m; i++)
pmp[i] = i*(4*3.14159/10);
for (i=0; i<m; i++)
ppp[i] = -i*(4*3.14159/10);
myarray[0] = 2.0;
Plot_Pointer[0] = pm;
Plot_Pointer[1] = pp;
Plot_Pointer[2] = fit_order;
mexCallMATLAB(0,NULL,3,Plot_Pointer,"polyfit");
This code compiles without issue, but causes Matlab to crash when executed. I am confident that it is an issue with the definition of Plot_Pointer, but I do not have enough knowledge of the subject at this point to figure out what. I am able to execute other mexGetMATLAB functions that only require 1 input, but have failed to figured out how to handle inputs that require more than one value. Any help would be appreciated.

Best Answer

Well, I managed to figure the problem out. The updated code is below :
Plot_Pointer[0] = *pm;
Plot_Pointer[1] = *pp;
Plot_Pointer[2] = *fit_order;
mexCallMATLAB(3,OutFit,3,Plot_Pointer,"polyfit");
Everything works as expected now. Just wanted to put the final solution out there in case anyone runs into the same problem that I did and wants to call the PolyFit function from MEX:
mxArray *rhs[1], *lhs[1], *pm[1], *pp[1], *fit_order[1];
mxArray *Fit_Pointer[3] ;
mxArray *OutFit[3];
pm[0] = mxCreateDoubleMatrix(m, n, mxREAL);
pp[0] = mxCreateDoubleMatrix(m, n, mxREAL);
fit_order[0] = mxCreateDoubleScalar(2.0);
pmp = mxGetPr(pm[0]);
ppp = mxGetPr(pp[0]);
for (i=0; i<m; i++)
pmp[i] = i*(4*3.14159/10);
for (i=0; i<m; i++)
ppp[i] = -i*(4*3.14159/10);
Fit_Pointer[0] = *pm;
Fit_Pointer[1] = *pp;
Fit_Pointer[2] = *fit_order;
mexCallMATLAB(3,OutFit,3,Fit_Pointer,"polyfit");