필터 지우기
필터 지우기

Passing struct to Mex file with C++ interface

조회 수: 6 (최근 30일)
Torsten Knüppel
Torsten Knüppel 2020년 2월 21일
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개)

카테고리

Help CenterFile Exchange에서 Write C++ Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by