Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ matPutVariable:尝试将数据输出到mat文件时出错(矩阵::序列化::错误大小)_C++_C_Matlab_Mex_Mat File - Fatal编程技术网

C++ matPutVariable:尝试将数据输出到mat文件时出错(矩阵::序列化::错误大小)

C++ matPutVariable:尝试将数据输出到mat文件时出错(矩阵::序列化::错误大小),c++,c,matlab,mex,mat-file,C++,C,Matlab,Mex,Mat File,我用C/C++创建了一个模拟器,该模拟器应该以.mat文件的形式输出结果,该文件可以导入到Matlab中的一些可视化工具中 在模拟过程中,结果存储在数据缓冲区中。缓冲区是一个std::map,其中字符串应与相应的matlab struct字段同名,而double*是缓冲数据 在模拟结束时,我使用以下代码将缓冲数据写入.mat文件 const char **fieldnames; // Declared and populated in another class method int numFi

我用C/C++创建了一个模拟器,该模拟器应该以.mat文件的形式输出结果,该文件可以导入到Matlab中的一些可视化工具中

在模拟过程中,结果存储在数据缓冲区中。缓冲区是一个
std::map
,其中字符串应与相应的matlab struct字段同名,而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);

我试图用一个简单的例子来重现这个错误,但我看不出问题所在。这是我的密码:

测试材料api.cpp
#包括“mat.h”
#包括
int main()
{
//输出MAT文件
MATFile*pmat=matOpen(“out.mat”,“w”);
//创建带有两个字段的标量结构数组
常量字符*字段名[2]={“a”,“b”};
mxArray*s=mxCreateStructMatrix(1,1,2,字段名);
//填充结构字段
对于(mwIndex i=0;i>mex-客户机引擎-LargearyDIMS测试\u map\u api.cpp
接下来,我运行可执行文件:

>!test\u map\u api.exe
最后,我在MATLAB中加载创建的MAT文件:

>whos-file out.mat
名称大小字节类属性
my_结构1x1 512结构
>>装船
>>我的结构
我的结构=
a:[10x1双精度]
b:[10x1双精度]
>>(我的结构b)'
ans=
1     1     1     1     1     1     1     1     1     1

所以一切都成功运行(我在Windowsx64上使用MatlabR2014A)很明显,这是一个独立的程序,不是一个MEX函数。对了。我在UNIX应用程序中使用MX和Mat C++库。谢谢你的研究。原来是我的一个同事编写的代码。他写了一个简单的脚本,只是想试试,我把它加入到实际应用中。对他来说,它是有效的,但是N。对我来说是ot。非常奇怪。我会继续研究它,看看是否能解决它。只是澄清一下,我在运行你的代码时遇到了相同的错误。我没有在新的(空)版本中尝试过它项目。可能是个主意。@FabianJonsson:上面的代码对我来说很好,所以可能你的代码中其他地方有问题……你的应用程序是多线程的吗?我问,因为MAT和MX API不是线程安全的。
-Wl,-rpath={matlab path}/bin/glnxa64