Clear Filters
Clear Filters

Passing struct to Mex file with C++ interface

6 views (last 30 days)
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

Answers (0)

Community Treasure Hunt

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

Start Hunting!