MEX Function - Using mexCallMATLAB function in conjunction with "PolyFit"

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.

 Akzeptierte Antwort

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");

Weitere Antworten (3)

Friedrich
Friedrich am 3 Dez. 2011

0 Stimmen

Hi,
not 100% sure but polyfit return always at least one variable which you don't capture. Maybe this causes the crash?
Friedrich,
I did notice that and updated the code as follows :
mxArray *rhs[1], *lhs[1], *pm[1], *pp[1], *fit_order[1];
mxArray *Plot_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);
Plot_Pointer[0] = pm;
Plot_Pointer[1] = pp;
Plot_Pointer[2] = fit_order;
mexCallMATLAB(3,OutFit,3,Plot_Pointer,"polyfit");
but still have the same issue. I am still trying some other changes, but still a little over my head right now.
You don't need to make pm, pp, and fit_order arrays. Try this:
double *pmp, *ppp;
mxArray *rhs[1], *lhs[1], *pm, *pp, *fit_order;
mxArray *Plot_Pointer[3] ;
mxArray *OutFit[3];
pm = mxCreateDoubleMatrix(m, n, mxREAL);
pp = mxCreateDoubleMatrix(m, n, mxREAL);
fit_order = mxCreateDoubleScalar(2.0);
pmp = mxGetPr(pm);
ppp = mxGetPr(pp);
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);
Plot_Pointer[0] = pm;
Plot_Pointer[1] = pp;
Plot_Pointer[2] = fit_order;
mexCallMATLAB(3,OutFit,3,Plot_Pointer,"polyfit");
Also, I would note that you are not setting the entire contents of pm and pp with data (they are sized m*n but you are only filling m elements) ... so the remainder is garbage which you then pass to polyfit.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by