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

3 Ansichten (letzte 30 Tage)
Rohan Leighton
Rohan Leighton am 16 Nov. 2023
Beantwortet: Ted am 17 Nov. 2023
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?

Antworten (1)

Ted
Ted am 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.

Kategorien

Mehr zu MATLAB Data API for C++ finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by