如何实现Eigen向量与MATLAB mex cell数组的指针式数据同步传递?
Solution: Map Eigen Vectors Directly to MATLAB mex Cell Array Memory
Got it, let's fix this issue. The core problem here is that you're trying to assign an Eigen::Map to an Eigen::VectorXd object, which won't share memory—instead, you need to use an array of Eigen::Map pointers to directly wrap the MATLAB mex array memory. This way, any modifications to the Eigen objects will immediately reflect in the y_output cell array without extra copies.
Here's the corrected code with explanations:
#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]; // Process input cell array (this part was fine) for(int t=0; t<T; t++){ mxArray* input_cell = mxGetCell(prhs[0], t); n[t] = mxGetNumberOfElements(input_cell); // Map input vector to Eigen (read-only or read-write, depending on your needs) z[t] = Eigen::Map<Eigen::VectorXd>(mxGetPr(input_cell), n[t]); } // Create output cell array mxArray* y_output = mxCreateCellMatrix(T, 1); // Use an array of Eigen::Map pointers to wrap mex array memory Eigen::Map<Eigen::VectorXd>* y = new Eigen::Map<Eigen::VectorXd>[T]; for(int t=0; t<T; t++){ // Create a MATLAB double vector in the cell mxArray* output_vec = mxCreateDoubleMatrix(n[t], 1, mxREAL); mxSetCell(y_output, t, output_vec); // Directly map the mex vector's memory to an Eigen::Map // This shares memory—changes to y[t] will update output_vec (and y_output) y[t] = Eigen::Map<Eigen::VectorXd>(mxGetPr(output_vec), n[t]); // Optional: Initialize to zero (you can do this via Eigen directly) y[t].setZero(); } // Now call your custom function—modifications to y[t] will auto-update y_output // Myfun(y, z); // Assign output plhs[0] = y_output; // Clean up allocated memory to avoid leaks delete[] z; delete[] y; delete[] n; }
Key Fixes & Explanations:
- Change
yto an array ofEigen::Mappointers: Instead ofEigen::VectorXd* y, we useEigen::Map<Eigen::VectorXd>* y.Eigen::Mapis a lightweight wrapper that doesn't allocate its own memory—it directly uses the memory of the MATLAB mex array. This is the critical part for shared memory access. - Properly bind each
y[t]to the mex array memory: After creatingoutput_vecwithmxCreateDoubleMatrix, we wrap its underlying data pointer (mxGetPr(output_vec)) withEigen::Map. Any writes toy[t](e.g.,y[t](0) = 5.0) will directly modify the memory ofoutput_vec, which is part ofy_output. - Memory cleanup: Don't forget to delete the dynamically allocated arrays (
z,y,n) to prevent memory leaks in your mex function.
Important Notes:
- Ensure that the MATLAB mex arrays (
output_vec) are created before creating theEigen::Map—the memory must exist and be valid for the Map to work. Eigen::Mapdoesn't take ownership of the memory, so you don't have to worry about it deleting the mex arrays (MATLAB manages those once you returny_outputas a plhs).- If your
MyfunexpectsEigen::VectorXd*instead ofEigen::Map<Eigen::VectorXd>*, you can adjust it—sinceEigen::Mapinherits fromEigen::VectorXd, it should be compatible (Eigen's API treats Maps the same as regular Vectors for most operations).
内容的提问来源于stack exchange,提问作者Bayes
相关产品推荐
相关产品推荐

