Interaction MATLAB-C++ and toolboxes

2 views (last 30 days)
J.L. Jones
J.L. Jones on 25 Jul 2014
Answered: Charu on 27 Jan 2025
Hi there, Sorry if this is a newbie question but I'd like to get my ideas straight before start doing what I want to.
I understand there's a way to use MATLAB code in C++ (i.e. calling it from C++). But my questions are:
- I have a bunch of functions all of which called by a script, is possible to call such script and retrieve the output. I can also wrap the script around another functions, but still, can I call a function that call another function (and so forth) even if those are in different files?
- I am in extreme needs of the image toolbox in my functions. Is this possible in the moment I call the code from C++?
- In which format I can obtain the output. Can I just map a MATLAB matrix in a C++ matrix?
- Can I pass the code an Image from C++ as a unsigned char matrix?
I understand there may be a documentation out there about this, but I found very fragmented sources, can you in case link me to a good one?
Thank you very much

Answers (1)

Charu
Charu on 27 Jan 2025
Yes, it is possible to call a MATLAB script or function from C++ and retrieve the output, even if they are in different files. To achieve this, the MATLAB Engine API can be utilized.
1. Ensure that the "MatlabEngine.hpp" header file, which is part of the C++ Engine API provided by MATLAB, is included. Functions can be invoked using the feval and fevalAsync member functions of the matlab::engine::MATLABEngine class.
2. The Image Processing Toolbox can be accessed within MATLAB functions when called from C++. Ensure that the Toolbox is installed and the MATLAB engine is initialized. The engEvalString function allows execution of commands in the MATLAB environment from a C++ application. The syntax is as follows:
#include engine.h
int engEvalString(Engine *ep, const char *string)
3. When executing MATLAB code from C++, the output is returned as mxArray objects, which are MATLAB's primary data representation. A MATLAB matrix can be mapped to a C++ matrix using the following approach:
%% Get matrix dimensions
mwSize numRows = mxGetM(matlabMatrix);
mwSize numCols = mxGetN(matlabMatrix);
%% Get pointer to data
double *data = mxGetPr(matlabMatrix);
%% Map to C++ vector of vectors
std::vector<std::vector<double>> cppMatrix(numRows, std::vector<double>(numCols));
for (mwSize i = 0; i < numRows; ++i) {
for (mwSize j = 0; j < numCols; ++j) {
cppMatrix[i][j] = data[i + j * numRows]; // Column-major to row-major
}
}
4. Images are typically represented as 2D or 3D arrays of unsigned char (for grayscale or RGB images). Load the image data in C++ as an unsigned char array. Use mxCreateNumericMatrix to create an mxArray to hold the image data. Copy the image data into the mxArray and use engPutVariable to pass the mxArray to the MATLAB workspace.
Here are the links for the documentation for the same:

Tags

Community Treasure Hunt

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

Start Hunting!