What is the Best Way to Call Matlab from a C++ Program and Return Results to that Program?

What is the best way to call Matlab from a C++ program and return results to that program?
So far, I can see two options:
1. Use "matlab -batch statement" with either system or popen command, have the statement write results to a file, and then read the file in the program.
2. Use the Engine API as described here: Call MATLAB Functions from C++ - MATLAB & Simulink
3. Anything else?
The first seems a bit easier, but have to deal with the file i/o. The second seems a nicer wrt avoiding the file i/o, but seems like a lot more code on the C++ side.
If it matters, Matlab function in mind is very much lightweight, i.e., just a couple of lines that return a couple of values.
Also, the example on the linked doc page has the line:
matlab::data::TypedArray<int16_t> result = matlabPtr->feval(u"gcd", args);
What is the u in the call to feval? Is it defined in one of the header files?

4 Comments

"Best" is in the eye of the beholder, but I'd offer expediency may trump elegance if it's just a throwaway application that won't be relied upon for long such that "quick-n-dirty" is morebetter™ than the embedded solution. If it's something for posterity, then it's probably worth the extra effort to build the code.
I don't know anything to speak of of C++ but I'd presume the "u" expression will translate to an unsigned address of the MATLAB function string when compiled and it will be in one of the zillion include files, probably #define somewhere.
If I had a compiler installed at the moment, I'd compile that example and look at the generated listing to see what it does evaluate to when compiled. Somebody who actually knows C++ will probably come along...
3. Anything else?
Depending on what your C++ code does, another option might be to turn it into a mex routine with a thin mexFunction( ) wrapper. Then use mexCallMATLAB( ) to make your MATLAB calls. But, again, this may or may not be easily accomplished depending on what your C++ code does. If it is just doing straightforward computations, then this could be easy. But if your C++ code is driving GUIs etc. then this might not be practical.
I would assume the u in the u"gcd" means Unicode, as opposed to "gcd" being a simple byte string.
Thanks for that idea, but it's not practical in my particular case. Any idea what the "u" is in the call to feval?
I think another option would be to use Matlab to convert the .m-function into a C-function (or C++ function if there's a difference) that can be linked to and called from the C++ program. I'm pretty sure there is a way to do that (w/ Matlab Coder?), but I think it would be overkill for my particular case.

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products

Release

R2024b

Asked:

on 15 Mar 2026

Edited:

on 16 Mar 2026

Community Treasure Hunt

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

Start Hunting!