I have a created a simulator in C/C++ that is supposed to output the results in a .mat file that can be imported into some visualization tools in Matlab.
During the simulation results are stored in a data buffer. The buffer is a std::map<const char *, double *>
, where the string should be the same name as the corresponding matlab struct field, and the double* is the buffered data.
At the end of the simulation I then use the following code to write the buffered data into a .mat file
const char **fieldnames; // Declared and populated in another class method
int numFields; // Declared in another method. Equal to fieldnames length.
int buffer_size; // Declared in another method. Equal to number of timesteps in simulation.
std::map<const char *, double *> field_data;
std::map<const char *, mxArray *> field_matrices;
// Open .mat file
MATFile *pmat = matOpen(filename.str().c_str(), "w");
// Create an empty Matlab struct of the right size
mxArray *SimData_struct = mxCreateStructMatrix(1,1,this->numFields,this->fieldnames);
int rows=this->buffer_size, cols=1;
for(int i=0; i<this->numFields; i++) {
// Create an empty matlab array for each struct field
field_matrices[this->fieldnames[i]] = mxCreateDoubleMatrix(rows, cols, mxREAL);
// Copy data from buffers to struct fields
memcpy(mxGetPr(field_matrices[this->fieldnames[i]]), this->field_data[this->fieldnames[i]], rows * cols * sizeof(double));
// Insert arrays into the struct
mxSetField(SimData_struct,0,this->fieldnames[i],field_matrices[this->fieldnames[i]]);
}
matPutVariable(pmat, object_name.str().c_str(), SimData_struct);
I can compile and start the simulation, but it dies with an error when the matPutVariable command is reached. The error I get is terminate called after throwing an instance of 'matrix::serialize::WrongSize'
. I have tried to google for more information, but have been unable to find something that could help me.
Mathworks support helped me to identify the cause of the issue. My application uses boost 1.55, but Matlab uses 1.49. There was a clash between those dependencies that was solved by adding an additional external dependencies directory path.
-Wl,-rpath={matlab path}/bin/glnxa64