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).
采纳的回答
更多回答(4 个)
#include <iostream>
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
void getXYpoints(double* x, double* y, size_t* dataSize);
void polyFit(double* x, double* y, double* poly_coefficients, size_t xy_size, size_t poly_size) {
using namespace matlab::engine;
using namespace matlab::data;
// Start MATLAB engine synchronously
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
// Create MATLAB data arrays
ArrayFactory factory;
TypedArray<double> mxArrayX = factory.createArray<double>({xy_size});
TypedArray<double> mxArrayY = factory.createArray<double>({xy_size});
for (size_t i = 0; i < xy_size; ++i) {
mxArrayX[i] = x[i];
mxArrayY[i] = y[i];
}
// Call polyfit and get the result
FutureResult<std::vector<Array>> result = matlabPtr->fevalAsync(u"polyfit", 3, {mxArrayX, mxArrayY, factory.createScalar<double>(poly_size)});
std::vector<Array> results = result.get();
TypedArray<double> poly_coefficients_array = results[0];
StructArray s = results[1];
TypedArray<double> mu = results[2];
// Extract polynomial coefficients
for (size_t i = 0; i < poly_coefficients_array.getNumberOfElements(); ++i) {
poly_coefficients[i] = poly_coefficients_array[i];
}
// Extract s and mu if needed
// auto s_field1 = s[0][u"field1"];
// auto mu_field1 = mu[0][u"field1"];
}
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) {
std::cout << "File Size is Zero, nothing to do" << std::endl;
return -2;
}
polyFit(x, y, p, dataSize, 12); // 12 is the order of the polynomials
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
Nidal Bazzi
2024-6-5
0 个投票
1 个评论
Hassaan
2024-6-5
@Nidal Bazzi I have updated my code above. Try re-run.
Nidal Bazzi
2024-6-5
0 个投票
1 个评论
Hassaan
2024-6-6
@Nidal Bazzi I have updated my code above. Try re-run.
Nidal Bazzi
2024-6-7
0 个投票
2 个评论
Nidal Bazzi
2024-6-7
Hassaan
2024-6-7
@Nidal Bazzi You are welcome.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
类别
在 帮助中心 和 File Exchange 中查找有关 Call MATLAB from C++ 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

