Matlab のいくつかの視覚化ツールにインポートできる .mat ファイルに結果を出力することになっている C/C++ でシミュレーターを作成しました。
シミュレーション中、結果はデータ バッファーに格納されます。バッファーはstd::map<const char *, double *>
で、文字列は対応する matlab 構造体フィールドと同じ名前である必要があり、double* はバッファーされたデータです。
シミュレーションの最後に、次のコードを使用して、バッファリングされたデータを .mat ファイルに書き込みます。
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);
シミュレーションをコンパイルして開始することはできますが、matPutVariable コマンドに達するとエラーが発生して終了します。私が得るエラーはですterminate called after throwing an instance of 'matrix::serialize::WrongSize'
。詳細についてグーグルで検索しようとしましたが、役立つ情報が見つかりませんでした。
Mathworks のサポートは、問題の原因を特定するのに役立ちました。私のアプリケーションはブースト 1.55 を使用していますが、Matlab は 1.49 を使用しています。これらの依存関係の間に衝突がありましたが、追加の外部依存関係ディレクトリ パスを追加することで解決されました。
-Wl,-rpath={matlab path}/bin/glnxa64