How to resize/append to a matlab::data:StructArray using the MATLAB C++ API
3 views (last 30 days)
Show older comments
I am implementing a function as a mex file in C++. In order to export internal data to MATLAB, I'm using the createStructArray function to preallocate a StructArray with n elements as below:
auto x = factory.createStructArray({ 1, n}, {"field1", "field2", "field3"});
I can then populate the fields of x something like this:
for (auto ii = 0; ii < n; ++ii) {
x[ii]["field1"] = factory.createScalar<double>(...);
x[ii]["field2"] = factory.createScalar<double>(...);
...
}
All good so far, however on a subsequent iteration of the function, I want to be able to append new data to an existing StructArray, which would require resizing the existing StructArray to, say, {1, n+n1}. Indexing outside of the preallocated range within MATLAB would automatically force a resize, however that doesn't work here. There doesn't appear to be any resize() or related functions in the API.
Is there a good way of resizing/appending new data to an existing StructArray that doesn't involve simply copying all of the existing elements to a new StructArray?
0 Comments
Answers (1)
Ted
on 17 Nov 2023
I believe there is no such convenience. As you said at the end of your question, you will have to make a new larger array and copy the existing elements over.
0 Comments
See Also
Categories
Find more on Call C++ from MATLAB 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!