How do I call Matlab polyfit function from C++?
Hello, I am trying to call the MATLAB polyfit function from a Windows application written in Visual Studio 2022 C++ and get the polynomial coefficients array, s, and mu from from polyfit.
int main(int argc, char* argv[])
{
double x[40];
double y[40];
double p[12]; // MATLAB returned array of coefficients
size_t dataSize;
// this function gets the data from a file and updates the unknown xy data size
// the data size = 40 elements
getXYpoints(x, y, &dataSize);
if (dataSize == 0)
{
cout << endl << "File Size is Zero, nothing to do";
return -2;
}
polyFit(x, y, p,dataSize, 12); // 12 is the order of the polynomials
}
The polyFit calls the MATLAB polyfit function
void polyFit(double* x, double* y, double* poly_coefficients, size_t xy_size, size_t poly_size)
{
using namespace matlab::engine;
// Start MATLAB engine synchronously
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
std::tuple<double, double, double> nresults;
// the following line gives a compile error for x, y, and the tuple definitions
nresults = matlabPtr->feval<std::tuple <double*, double, double>>(u"polyfit", x , y, poly_size);
//[p, s, mu] = polyfit (x,y, 12);
double P;
double S;
double MU;
std::tie(P, S, MU) = nresults;
for (auto it : p)
{
p[it] = P[it];
}
auto s = S;
auto mu = MU;
.
.
.
}
tuple <double*, double, double> tuple does not seem to support the first argument double*. Is there a better way to call polyfit from C++, or how do I overload the tuple class to accept a pointer to P (the polynomial coefficients array).Hello, I am trying to call the MATLAB polyfit function from a Windows application written in Visual Studio 2022 C++ and get the polynomial coefficients array, s, and mu from from polyfit.
int main(int argc, char* argv[])
{
double x[40];
double y[40];
double p[12]; // MATLAB returned array of coefficients
size_t dataSize;
// this function gets the data from a file and updates the unknown xy data size
// the data size = 40 elements
getXYpoints(x, y, &dataSize);
if (dataSize == 0)
{
cout << endl << "File Size is Zero, nothing to do";
return -2;
}
polyFit(x, y, p,dataSize, 12); // 12 is the order of the polynomials
}
The polyFit calls the MATLAB polyfit function
void polyFit(double* x, double* y, double* poly_coefficients, size_t xy_size, size_t poly_size)
{
using namespace matlab::engine;
// Start MATLAB engine synchronously
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
std::tuple<double, double, double> nresults;
// the following line gives a compile error for x, y, and the tuple definitions
nresults = matlabPtr->feval<std::tuple <double*, double, double>>(u"polyfit", x , y, poly_size);
//[p, s, mu] = polyfit (x,y, 12);
double P;
double S;
double MU;
std::tie(P, S, MU) = nresults;
for (auto it : p)
{
p[it] = P[it];
}
auto s = S;
auto mu = MU;
.
.
.
}
tuple <double*, double, double> tuple does not seem to support the first argument double*. Is there a better way to call polyfit from C++, or how do I overload the tuple class to accept a pointer to P (the polynomial coefficients array). Hello, I am trying to call the MATLAB polyfit function from a Windows application written in Visual Studio 2022 C++ and get the polynomial coefficients array, s, and mu from from polyfit.
int main(int argc, char* argv[])
{
double x[40];
double y[40];
double p[12]; // MATLAB returned array of coefficients
size_t dataSize;
// this function gets the data from a file and updates the unknown xy data size
// the data size = 40 elements
getXYpoints(x, y, &dataSize);
if (dataSize == 0)
{
cout << endl << "File Size is Zero, nothing to do";
return -2;
}
polyFit(x, y, p,dataSize, 12); // 12 is the order of the polynomials
}
The polyFit calls the MATLAB polyfit function
void polyFit(double* x, double* y, double* poly_coefficients, size_t xy_size, size_t poly_size)
{
using namespace matlab::engine;
// Start MATLAB engine synchronously
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
std::tuple<double, double, double> nresults;
// the following line gives a compile error for x, y, and the tuple definitions
nresults = matlabPtr->feval<std::tuple <double*, double, double>>(u"polyfit", x , y, poly_size);
//[p, s, mu] = polyfit (x,y, 12);
double P;
double S;
double MU;
std::tie(P, S, MU) = nresults;
for (auto it : p)
{
p[it] = P[it];
}
auto s = S;
auto mu = MU;
.
.
.
}
tuple <double*, double, double> tuple does not seem to support the first argument double*. Is there a better way to call polyfit from C++, or how do I overload the tuple class to accept a pointer to P (the polynomial coefficients array). polyfit, c++, visual studio c++, call matlab from c++ MATLAB Answers — New Questions