Clear Filters
Clear Filters

How to import the C++array into matlab workspace by matlab engine?

13 views (last 30 days)
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'.

Answers (1)

Avni Agrawal
Avni Agrawal on 4 Sep 2024 at 13:41
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);
  • 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"`.
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 Comment
James Tursa
James Tursa on 4 Sep 2024 at 22:51
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';");

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!