如何使用 "Matlab Data / Engine API for c++" 在 c++ 中加载 .mat



我正在尝试在C++程序中包含一个.mat。但是,由于 Matlab API for C (https://de.mathworks.com/help/matlab/matlab-c-api-to-read-mat-file-data.html( 和 for C++ API (https://de.mathworks.com/help/matlab/matlab-data-array.html( 之间的内部兼容性问题,我无法使用常规"mat.h"来执行此操作。有人知道使用新 API 实现此目的的方法吗?

提前感谢! :)

好的,我得到了一个相当欠佳的解决方案。对于那些可能也遇到这个问题的人,这是我到目前为止得到的:

#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
using namespace matlab::engine;
// Start MATLAB engine synchronously
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
std::vector<double> labels_cpp;
// Create figure window
matlabPtr->eval(convertUTF8StringToUTF16String("m = matfile('labelsOfGist.mat'); labels = m.labels;"));
//Get figure handle and Units property
matlab::data::ArrayFactory factory;
matlab::data::TypedArray<double> labels = matlabPtr->
getVariable(convertUTF8StringToUTF16String("labels"));
size_t elements = labels.getNumberOfElements();
labels_cpp.reserve(elements);
std::insert_iterator<std::vector<double>> insert_it(labels_cpp, labels_cpp.begin());
std::copy(labels.begin(),labels.end(), insert_it);

如果有人为此找到更好的解决方案,请告诉我:)

最新更新