How to convert C struct from mxArray pointer of structs?

4 views (last 30 days)
If I have in C file
struct FooInner {
double Bar;
int32 Baz;
};
struct Foo {
struct FooInner inner;
....
};
which is called via Matlab SDK C Compiler with following code:
if (mlxDo_smth_api(n_out, out, n_in, in))
{
mxArray *fooArrPtr = mxGetData(out[0]);
for (int i = 0; i < 100; i++)
{
struct FooInner *fooInnerPtr = (struct FooInner *) mxGetPr(fooArrPtr);
printf("Bar = %f, baz = %d\n", fooInnerPtr->Bar, fooInnerPtr->Baz);
}
}
then I'm getting random numbers. I know how to parse a single struct fields from Matlab (need to mxGetData and cast it to proper struct field data type), however, how to do with array of structs? I can't increment fooArrPtr as well -- giving me error that I can't increment mxArray.
  2 Comments
Sabyrzhan Tasbolatov
Sabyrzhan Tasbolatov on 8 Aug 2022
I'm gathering information from Matlab function like
function [output] = MyFunc(in1, in2)
..
output = struct('inner', {}, 'smth', {})
..
output{ii}.inner = inner_result
end
Thanks for pointing out not to mix mxArray with C struct. Will reformat to collect result into flatten array of values, rather than array of structs for output.

Sign in to comment.

Answers (0)

Categories

Find more on Troubleshooting in MATLAB Compiler SDK in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by