Passing struct to Mex file with C++ interface

5 vues (au cours des 30 derniers jours)
Torsten Knüppel
Torsten Knüppel le 21 Fév 2020
Hi all,
I'm trying to pass a struct to a Mex-File and would like to know what is the suggested way to access its fields.
Here is a short script for a illustrating the problem:
mex mexStructTest.cpp
strct = struct('a', 10);
mexStructTest(strct);
The corresponding Mex-file is
#include "mex.hpp"
#include "mexAdapter.hpp"
using namespace matlab::data;
using matlab::mex::ArgumentList;
using matlab::engine::convertUTF8StringToUTF16String;
class MexFunction : public matlab::mex::Function {
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
if(inputs[0].getType() == matlab::data::ArrayType::STRUCT){
// Working
StructArray s(inputs[0]);
matlab::data::Reference<Struct> s2 = s[0];
// Struct s2 = StructArray(inputs[0])[0];
// Not working -> Crash
// matlab::data::Reference<Struct> s2 = StructArray(inputs[0])[0];
// Struct s2 = StructArray(inputs[0])[0];
double a = TypedArray<double>(s2["a"])[0];
std::cout << a << "\n";
}
}
};
The way I managed to get it working requires to first use a StructArray and then access its fields. However, I would prefer to have this thing as a one-liner (one of the options in the not-working category) or at least understand, why it is crashing.
Thanks in advance and best regards,
Torsten

Réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by