Mex mxCreateStructMatrix and mxSetFieldByNumber massive Matlab workspace byte use.
Show older comments
I am trying to write a mex c file which creates a structure in matlab. 59mB of structure in C of 6 uint32's becomes 1.5GBs structure in matlab.
I don't know where I'm going wrong with this code.
plhs[13] = mxCreateStructMatrix(1, gyro_time_mark.size(), gps_time_field_count, &gps_time_names_pointers[0]);
for (int i = 0; i < gyro_time_mark.size(); i++) {
uint32_t* offset = (uint32_t*)&(gyro_time_mark[i]);
for (int j = 0; j < gps_time_field_count; j++){
mxArray *field_value;
field_value = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL);
uint32_t* copy = (uint32_t*)mxCalloc(1, sizeof(uint32_t));
std::memcpy(copy, offset, sizeof(int32_t));
offset++;
mxSetData(field_value, copy);
mxSetFieldByNumber(plhs[13], i, j, field_value);
}
}
The data in Matlab is correct, its just that the structure in matlab is 1.5GB in size when it should be 50MB. The number of elements is correct and the structure field count and inside types are correct. However the data use is way way above what I would expect.
Instead of a creating a structure , I tried creating an mx6 array and that did come out to ~50mB which is exactly what I would expect. How am I using mxCreateStructMatrix and mxSetFieldByNumber incorrectly?
Accepted Answer
More Answers (0)
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) 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!