MEX Function - Using mexCallMATLAB function in conjunction with "PolyFit"
Show older comments
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.
Accepted Answer
More Answers (3)
Friedrich
on 3 Dec 2011
0 votes
Hi,
not 100% sure but polyfit return always at least one variable which you don't capture. Maybe this causes the crash?
Michael Stackpole
on 3 Dec 2011
James Tursa
on 5 Dec 2011
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.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!