Trouble with matlab::data::Array

20 views (last 30 days)
Jeroen Boschma
Jeroen Boschma on 7 Jul 2023
Edited: Jeroen Boschma on 10 Jul 2023
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?

Answers (1)

Steven Lord
Steven Lord on 7 Jul 2023
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 Comment
Jeroen Boschma
Jeroen Boschma on 10 Jul 2023
Edited: Jeroen Boschma on 10 Jul 2023
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.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!