Trouble with matlab::data::Array

20 次查看(过去 30 天)
The Array/StructArray is still very shady to me (why is there not (I cannot find...) a basic explanation from matlab instead of a handfull examples that just do not tell you what the whole idea behind Arrays, TypedArrays and StructArrays is?)....
I have the following:
matlab::data::Array myData;
void doSomethingWithElement(const matlab::data::Array& matlabArray) { ... }
for (size_t i = 0; i < myData.getNumberOfElements(); i++)
doSomethingWithElement(myData[i]);
This seems to crash at 'myData[i]' (tested separately) with the message 'Field does not exist in this struct.'. A separate test also crashes:
matlab::data::Array dummy = myData[0];
What am I doing wrong? Do I have to toss in 'matlab::data::ArrayFactory::createArray' in between?

回答(1 个)

Steven Lord
Steven Lord 2023-7-7
Have you seen the first of the Topics on this documentation page, which I believe is a "basic explanation" of the C++ MATLAB Data API?
If the data you're working with is a MATLAB struct array you may want to use a matlab::data::StructArray (created from a matlab::data:ArrayFactory using its createStructArray method) instead of a plain matlab::data::Array.
I think you may be able to adapt the "Access Struct Array Data" example on this documentation page to retrieve the appropriate fields of the appropriate elements in your struct and pass them into you doSomethingWithElement function.
  1 个评论
Jeroen Boschma
Jeroen Boschma 2023-7-10
编辑:Jeroen Boschma 2023-7-10
Thanks for your answer Steve, and sorry if my wording was a bit 'frustrated'.... The approach for the described function is 'legacy' in the project I'm working on. The problem is apparently that if you take a field field from a structure and field is a vector itself, then it is impossible to convert each element of field back to a plain matlab::data::Array and have it processsed by a function that accepts that array. Once in StructArray you can't go back (I did not find a way...). That means that I have to rewrite the doSomethingWithElement to accept a StructArray and an index in order to convert a single element. Bottom line is then that writing conversion routines accepting matlab::data::Array is a bad choice.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by