How to pass a vector<float> (1D) from C++ into MATLAB for plotting?

16 vues (au cours des 30 derniers jours)
Angad Bajwa
Angad Bajwa le 2 Sep 2020
I'm having some trouble with creating a MATLAB data array in a C++ environment that is composed of data from a vector<float> (1-dimensional) - I don't need to work with this data in MATLAB itself as there is no processing, in this case I'm just using it for it's plotting capabilities.
So far I know that you can use createArray (called on an arrayFactory object) to create a MATLAB array, but this only accepts an initializer_list as an argument, not a dynamic array.
For some context, once it is a workable array in MATLAB I plan to use the eval function to plot it directly from the C++ environment.

Réponses (1)

Uday Pradhan
Uday Pradhan le 7 Sep 2020
Hi Angad,
You can use "createArray" with C++ vector iterators as well. Please refer to this link for more details. I have created a small example of how you can use a C++ vector of doubles and plot it in MATLAB itself.
#include <iostream>
#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
int main()
{
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
//Create MATLAB data array factory
matlab::data::ArrayFactory factory;
std::vector <double> vec(4);
vec[0] = 1.0,vec[1] = 2.0,vec[2] = -9.2,vec[3] = -0.9;
int dim = vec.size();
matlab::data::TypedArray<double> const argArray = factory.createArray({1,dim},vec.begin(),vec.end());
matlabPtr->feval(u"plot", argArray);
return 0;
}
Hope this helps!

Catégories

En savoir plus sur Data Exchange and Mapping with C++ Applications dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by