- mxCreateDoubleMatrix: This function creates a 2D array (matrix) in MATLAB. The dimensions (2, 3) correspond to the dimensions of your C++ array.
- memcpy: This is used to copy the C++ array data into the `mxArray`.
- engPutVariable: This function sends the `mxArray` to the MATLAB workspace with the name `"myMatlabArray"`.
How to import the C++array into matlab workspace by matlab engine?
조회 수: 8 (최근 30일)
이전 댓글 표시
Currently I am using the Matlab Engine in visual studio and there is a two dimensional array in my cpp file.The question is how I can import the array to matlab workspace by the function of 'engPutVariable'.
댓글 수: 0
답변 (1개)
Avni Agrawal
2024년 9월 4일
I understand that you are trying to use the `engPutVariable` function to transfer a two-dimensional array from your C++ application to the MATLAB workspace, you need to follow these steps:
1. Initialize the MATLAB Engine: Make sure that the MATLAB Engine is properly initialized in your C++ application.
2. Create a mxArray: Convert your C++ two-dimensional array into a `mxArray`, which is the data type MATLAB uses for arrays.
3. Use engPutVariable: Transfer the `mxArray` to the MATLAB workspace using `engPutVariable`.
Here is a step-by-step example to illustrate the process:
1. Include Necessary Headers:
Make sure to include the MATLAB Engine and Matrix API headers in your C++ file.
#include "engine.h"
#include "matrix.h"
2. Initialize the MATLAB Engine:
Start the MATLAB Engine session.
Engine *ep;
if (!(ep = engOpen(NULL))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
}
3. Create a Two-Dimensional Array:
Suppose you have a 2D array in C++:
double myArray[2][3] = { {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0} };
4. Convert to mxArray:
You need to convert this C++ array to a `mxArray`.
mxArray *matlabArray = mxCreateDoubleMatrix(2, 3, mxREAL);
memcpy(mxGetPr(matlabArray), myArray, 2 * 3 * sizeof(double));
5. Transfer to MATLAB Workspace:
Use `engPutVariable` to put the `mxArray` into the MATLAB workspace.
if (engPutVariable(ep, "myMatlabArray", matlabArray)) {
fprintf(stderr, "\nFailed to put variable into MATLAB workspace\n");
return EXIT_FAILURE;
}
6. Clean Up:
After you're done, make sure to clean up and close the MATLAB Engine session.
mxDestroyArray(matlabArray);
engClose(ep);
Make sure to link against the MATLAB Engine and Matrix libraries in your Visual Studio project settings to ensure successful compilation and linking.
I hope this helps!
댓글 수: 1
James Tursa
2024년 9월 4일
This answer neglects the fact that C/C++ 2D arrays are row major, but MATLAB 2D arrays are column major. So the code above will copy the elements in a jumbled state. There are various ways to deal with this. One way is to copy into a transposed array (3x2 in this case) and then transpose the result to a 2x3. E.g.,
mxArray *matlabArray = mxCreateDoubleMatrix(3, 2, mxREAL);
To transpose the result in the Engine workspace after the transfer, simply call:
engEvalString(ep, "myMatlabArray = myMatlabArray';");
참고 항목
카테고리
Help Center 및 File Exchange에서 Call MATLAB from C에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!