How to resize/append to a matlab::data:StructArray using the MATLAB C++ API

3 次查看(过去 30 天)
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?

回答(1 个)

Ted
Ted 2023-11-17
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB Data API for C++ 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by