フィルターのクリア

How to send std::map from Mex function back to Matlab?

4 ビュー (過去 30 日間)
Rakesh Sadhu
Rakesh Sadhu 2021 年 10 月 15 日
回答済み: Rishav 2024 年 2 月 27 日
class MexFunction : public matlab::mex::Function {
public:
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs)
{
ArrayFactory factory;
std::map<int, std::string > test_map {
{1, "Bla-bla"},
{2, "cla-cla"},
{3, "dla-dla"},
};
outputs[0] = ... // how to pass back test_map?
}
};

回答 (1 件)

Rishav
Rishav 2024 年 2 月 27 日
Hi Rakesh,
To send a std::map from a Mex function back to MATLAB, it needs to be converted into a MATLAB data type that MATLAB can understand.
Here is the updated code to do the same:
class MexFunction : public matlab::mex::Function {
public:
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
matlab::data::ArrayFactory factory;
std::map<int, std::string> test_map {
{1, "Bla-bla"},
{2, "cla-cla"},
{3, "dla-dla"},
};
// Convert std::map to MATLAB cell array
matlab::data::CellArray cellArray = factory.createCellArray({1, test_map.size()});
int idx = 0;
for (const auto& pair : test_map) {
cellArray[idx++] = factory.createCharArray(pair.second.c_str());
}
// Assign the MATLAB cell array to the output
outputs[0] = cellArray;
}
};
Also, make sure to include the appropriate headers for the MATLAB Data API.

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by