简体   繁体   English

将 std::complex 值的 boost::multi_array 复制到 mxArray

[英]Copy boost::multi_array of std::complex values to mxArray

I am trying to copy a boost::multi_array of std::complex values to an mxArray so I can write the values out to MATLAB .mat file.我正在尝试将 std::complex 值的 boost::multi_array 复制到 mxArray 以便我可以将值写入 MATLAB .mat 文件。 When I load the .mat file into MATLAB, it only has the real portions of the data and none of the imaginary portions.当我将 .mat 文件加载到 MATLAB 中时,它只有数据的实部,没有虚部。 Here is a code snippet showing what I am doing.这是一个代码片段,显示了我在做什么。

boost::multi_array<std::complex<double>, 3> SAP_max_combined_complex(boost::extents[1][1][1], boost::fortran_storage_order());

// Later in the code
SAP_max_combined_complex.resize(boost::extents[xDim][yDim][zDim]);
// The multi_array is populated correctly - this has been verified

mwSize numDims = SAP_max_combined_complex.num_dimensions();
mwSize outputSizes[3] = {SAP_max_combined_complex.shape()[0], SAP_max_combined_complex.shape()[1], SAP_max_combined_complex.shape()[2]};
mxArray* outputData = mxCreateNumericArray(numDims, outputSizes, mxDOUBLE_CLASS, mxCOMPLEX);
memcpy(mxGetData(outputData), (void*) SAP_max_combined_complex.data(), (sizeof(std::complex<double>) * SAP_max_combined_complex.num_elements()));

// I then write the mxArray to the file with
matPutVariable(mpMatFile, fieldName.toStdString().c_str(), outputData);

Also of interesting note is if when I make the memcpy call, the deallocation of SAP_max_combined_complex crashes when I terminate my application.另外一个有趣的注意事项是,如果当我进行 memcpy 调用时,SAP_max_combined_complex 的解除分配在我终止我的应用程序时崩溃。 If I comment out the memcpy call, the deallocation works properly (or at least doesn't crash).如果我注释掉 memcpy 调用,则解除分配工作正常(或至少不会崩溃)。

Any ideas what I am doing wrong?任何想法我做错了什么? Thanks!谢谢!

Matlab stores the real and imaginary part as separate arrays. Matlab 将实部和虚部存储为单独的数组。 mxGetData() only returns the pointer to the real part! mxGetData() 只返回指向实部的指针!

Your approach on the other hand (mainly because of std::complex), stores real and imaginary part as pairs.另一方面,您的方法(主要是因为 std::complex)将实部和虚部成对存储。

You need to untangle the pairs of real and imaginary parts and write them in separate arrays (real part goes to mxGetData() and imag part to mxGetImagData()).您需要解开实部和虚部对并将它们写入单独的数组中(实部转到 mxGetData(),而 imag 部分转到 mxGetImagData())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM