Can I use the Matlab engine in a non-mex library?
2 views (last 30 days)
Show older comments
Jeroen Boschma
on 23 Jun 2023
Commented: Jeroen Boschma
on 24 Jun 2023
The working situation is as follows:
- a library 'CPP_LIB' with ordinary C++ functions is compiled and built
- a mex-file is generated, it calls functions from 'CPP_LIB' so the mex is linked to 'CPP_LIB'
This works, but: now I want to be able to use the Matlab engine and call Matlab functions from within 'CPP_LIB'. That does not work, I get all sorts of compiler errors as soon as I even include <mex.hpp> and <mexAdapter.hpp>. Is there any way to do this?
2 Comments
James Tursa
on 23 Jun 2023
Edited: James Tursa
on 23 Jun 2023
It is not clear exactly what you are doing. What do you mean "... call MATLAB functions from within CPP_LIB ..."? How are you doing this calling exactly? If CPP_LIB is a normal C++ library, how is it trying to call mex functions? Is CPP_LIB the code that is invoking the MATLAB Engine?
The MATLAB Engine should be able to call mex routines the same as any other function. Just compile the mex routines normally and call them via engEvalString( ). But note that the architecture for this is that the MATLAB Engine is a separate process that does not share memory with your C++ code that is invoking the Engine ... meaning all variables must be deep copied back and forth, chewing up time and memory. If your variables are huge then this may not be the best approach.
Accepted Answer
Nandini
on 23 Jun 2023
Integrating the MATLAB Engine functionality within a C++ library (CPP_LIB) can be challenging due to the differences in compilation requirements and dependencies between MATLAB's MEX files and standard C++ code. The MATLAB Engine is primarily designed to be used within MATLAB or to run MATLAB code as standalone applications.
However, if you still want to call MATLAB functions from within your CPP_LIB library, one approach is to separate the MATLAB-specific code into a separate MEX file and communicate between the library and the MEX file using function calls or other suitable inter-process communication mechanisms. Here's a general outline of how you can approach this:
1. Create a separate MEX file (let's call it "mexFunction.cpp") that includes the MATLAB Engine headers and contains the code to call MATLAB functions.
cpp code-
#include "mex.h"
#include "engine.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Initialize MATLAB Engine
Engine *ep;
ep = engOpen(NULL);
if (ep == NULL)
{
mexErrMsgIdAndTxt("MATLAB:error", "Failed to open MATLAB Engine");
}
// Call MATLAB functions using the Engine
engEvalString(ep, "disp('Hello from MATLAB Engine!')");
// Close MATLAB Engine
engClose(ep);
}
2. Compile the "mexFunction.cpp" file to generate a MEX file using the MATLAB MEX compiler. You can do this by running the following command in MATLAB:
matlab code-
mex mexFunction.cpp -lmat -leng
3. Link the generated MEX file to your CPP_LIB library by including the MEX file in your library's build process and specifying the appropriate linking options.
4. From within the CPP_LIB library, you can call the MEX function as needed to interact with MATLAB. You can do this by using mechanisms like function pointers or other inter-process communication methods to invoke the MEX function from within your library.
Keep in mind that integrating MATLAB and C++ in this manner can be complex, and there may still be limitations and challenges due to the different compilation and runtime environments. It's important to carefully manage memory, handle errors, and ensure proper initialization and termination of MATLAB Engine instances to avoid potential issues.
Additionally, consider the licensing requirements for the MATLAB Engine and MEX files, as they may have restrictions on distribution and deployment. Consult the MATLAB documentation and MathWorks support for more information and guidance on using the MATLAB Engine in your specific scenario.
More Answers (0)
See Also
Categories
Find more on Call MATLAB from C in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!