Passing struct to Mex file with C++ interface
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!