在带有单元格数组的C 特征矩阵和MATLAB MXARRAY之间传递数据
pass data between C++ Eigen Matrix and MATLAB mxArray with a cell array
我想传递eigen矩阵/矢量和mex数组之间的数据。在以下代码中,我定义了一个名为y_output的MEX数组,该数组包含一个单元格数组。变量y_output将传递给MATLAB。y_output中的每个元素都是向量,但长度不同。我想将指向特征向量指向MEX数组y_output的指针。
请注意,存储在Y中的数据将通过用户定义的函数进行修改。调用该函数后,我假设存储在y_output中的数据将被修改相应。但是,我无法将指针从y_output直接传递到y。有什么办法使它成为可能吗?谢谢!
这个问题与PASS C 特征矩阵到MATLAB MEX输出相似,但与Pass C 特征矩阵的问题不同。这个问题是问如何通过一系列矩阵,而该链接中的问题是在询问如何通过矩阵。
#include "mex.h"
#include "matrix.h"
#include <Eigen>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// prhs[0]: a cell array of length T, each element is a vector with different lengths
mwSize T = mxGetNumberOfElements(prhs[0]);
mwSize* n = new mwSize[T];
Eigen::VectorXd* z = new Eigen::VectorXd[T];
for(int t=0; t<T; t++){
n[t] = mxGetNumberOfElements(mxGetCell(prhs[0], t));
z[t] = Eigen::Map<Eigen::VectorXd>(mxGetPr(mxGetCell(prhs[0], t)), n[t]);
}
// create a cell matrix with T rows and one columns
mxArray* y_output = mxCreateCellMatrix(T,1);
// create corresponding Eigen objects
Eigen::VectorXd* y = new Eigen::VectorXd[T]();
Eigen::VectorXd y_temp(n[0]); y_temp.setZero();
for(int t=0; t<T; t++){
mxSetCell(y_output, t, mxCreateDoubleMatrix(n[t], 1, mxREAL));
y[t] = Eigen::VectorXd::Zero(n[t]);
y_temp.resize(n[t]);
Eigen::Map<Eigen::VectorXd> y[t](mxGetPr(mxGetCell(y_output, t)), n[t]); // This is not correct!
}
// Myfun(y, z);
// set output
plhs[0] = y_output;
}
您的解决方案将围绕MyFunc
输入和输出的数据复制。可以使用Eigen::Map
对象的std::vector
传递两个参数。以下代码基于您的答案。以//--
开头的行已从您的代码中删除,并由随后的行取代。
作为旁注:避免使用new
分配数组(在所有情况的99.99%中),但使用std::vector
。当对象脱离范围时,这要处理所有资源。另外,在MyFunc
中,您不必猜测y
和z
的大小。
#include "mex.h"
#include "matrix.h"
//-- #include <Eigen> <-- this should be <Eigen/Eigen>, but likely Eigen/Core suffices
// Perhaps you also need to change your include-path
#include <Eigen/Core>
#include <vector>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// prhs[0]: a cell array of length T, each element is a vector with different lengths
mwSize T = mxGetNumberOfElements(prhs[0]);
//-- mwSize* n = new mwSize[T];
std::vector<mwSize> n(T);
//-- Eigen::VectorXd* z = new Eigen::VectorXd[T];
std::vector<Eigen::Map<const Eigen::VectorXd> > z; // input vector of Maps
z.reserve(T);
for(int t=0; t<T; t++){
// Note: You don't actually seem to need n[t], except for creating y_output
n[t] = mxGetNumberOfElements(mxGetCell(prhs[0], t));
//-- z[t] = Eigen::Map<Eigen::VectorXd>(mxGetPr(mxGetCell(prhs[0], t)), n[t]);
z.emplace_back(mxGetPr(mxGetCell(prhs[0], t)), n[t]);
}
// create a cell matrix with T rows and one columns
mxArray* y_output = mxCreateCellMatrix(T,1);
// create corresponding Eigen objects
//-- Eigen::VectorXd* y = new Eigen::VectorXd[T]();
std::vector<Eigen::Map<Eigen::VectorXd> > y; // output vector of Maps
y.reserve(T);
// This must be called after setting up y:
//-- Myfun(y, z);
//-- double* ptemp;
for(int t=0; t<T; t++){
mxSetCell(y_output, t, mxCreateDoubleMatrix(n[t], 1, mxREAL));
//-- ptemp = mxGetPr(mxGetCell(y_output, t));
//-- // assign the data stored in y[t] to the contents in y_output.
//-- for(int i=0; i<n[t]; i++){
//-- //mxGetPr(mxGetCell(y_output, t))[i] = y[t](i);
//-- ptemp[i] = y[t](i);
//-- }
y.emplace_back(mxGetPr(mxGetCell(y_output, t)), n[t]);
}
// Now call Myfun, but the function now needs to accept vectors of Eigen::Map, instead of pointers to VectorXd
// It should be possible to keep the Code inside Myfun unchanged
// Myfun(y, z);
//-- ptemp = NULL;
// set output
plhs[0] = y_output;
}
我已经找到了一种实现我想要的方法的方法。关键步骤是首先调用我自己的功能,最后,将y
中存储的数据分配给y_output
。我在下面包含了修改的代码,以便它对感兴趣的任何人都有帮助。
#include "mex.h"
#include "matrix.h"
#include <Eigen>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// prhs[0]: a cell array of length T, each element is a vector with different lengths
mwSize T = mxGetNumberOfElements(prhs[0]);
mwSize* n = new mwSize[T];
Eigen::VectorXd* z = new Eigen::VectorXd[T];
for(int t=0; t<T; t++){
n[t] = mxGetNumberOfElements(mxGetCell(prhs[0], t));
z[t] = Eigen::Map<Eigen::VectorXd>(mxGetPr(mxGetCell(prhs[0], t)), n[t]);
}
// create a cell matrix with T rows and one columns
mxArray* y_output = mxCreateCellMatrix(T,1);
// create corresponding Eigen objects
Eigen::VectorXd* y = new Eigen::VectorXd[T]();
// Myfun(y, z);
double* ptemp;
for(int t=0; t<T; t++){
mxSetCell(y_output, t, mxCreateDoubleMatrix(n[t], 1, mxREAL));
ptemp = mxGetPr(mxGetCell(y_output, t));
// assign the data stored in y[t] to the contents in y_output.
for(int i=0; i<n[t]; i++){
//mxGetPr(mxGetCell(y_output, t))[i] = y[t](i);
ptemp[i] = y[t](i);
}
}
ptemp = NULL;
// set output
plhs[0] = y_output;
}
相关文章:
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 有可能在Armadillo中复制MATLAB circshift方法吗
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- vector<vector<double>> to mxArray using memcpy
- MATLAB to C++: csvread() not supported by MATLAB Coder
- 加载由 MATLAB Coder 生成的带有函数的 DLL,该函数调用外部函数
- MATLAB:跟踪imufilter对象中的状态变化
- 如何将 c++ 中的客户端 TCP 的替身列表发送到 Matlab 中的 TCP 服务器?
- 将三角函数的正确值与matlab进行比较
- 连接 MATLAB 和 Visual Studios 的问题
- 如何将C++连接到 Matlab
- 如何发送 Mat H=findHomography 返回 Matlab
- 如何在C++中编写 MATLAB fix(X) 函数?
- 提供变量作为 MATLAB 系统命令的输入参数,以便C++可执行文件
- 有没有更好的方法可以使用特征/C++实现 matlab 的逻辑索引?
- 当我运行MEX文件时,MATLAB崩溃
- 在带有单元格数组的C 特征矩阵和MATLAB MXARRAY之间传递数据
- 使用 Matlab Coder 将 Matlab m 文件转换为 C/C++ 代码,包括 mex 文件 (mxArray
- 将多维C数组复制到Matlab中的mxArray类型
- matlab c共享库:用mxArray*/mxArray**捕获matlab函数输出