printing message to MATLAB command window with C++ Engine API.

7 views (last 30 days)
Hello All,
I am using the C++ engine api to connect to MATLAB. Is there a way to print messages to the MATLAB command windows from the C++ API (eg using matlabengine->eval , matlabengine->feval etc?).
I have tried using
matlabEngine->eval(u"disp(\"test2\")", nullptr, nullptr);
matlabEngine->eval(u"fprintf(\"test2\")", nullptr, nullptr);
Which, even though I do not specify standard out and error streams, does not print to the command window.
When I do specify output streams - I do capture the message output in c++.
The Reason for this is there are clib functions that we call from within the matlab command window, and some of these functions have associated messages - that we would like to print out interactively.
Thanks,
Trevor

Answers (1)

Tejas
Tejas on 24 Apr 2024
Hello Trevor,
It seems you are looking for a method to display messages in the MATLAB command line while utilizing the C++ Engine API, specifically using the eval or feval functions.
The process is outlined as follows:
  • Begin by creating a class that extends ‘matlab::mex::Function’. Within this class, establish a pointer to the C++ Engine API and instantiate an ‘std::ostringstream’ object.
class MexFunction : public matlab::mex::Function {
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
ArrayFactory factory;
std::ostringstream stream;
  • Proceed to create a method that overloads the parenthesis operator, enabling the class to be invoked from the MATLAB command line as if it were a function. In this method, incorporate the data intended for display on the MATLAB command line into the ‘ostringstream’ object. Then, execute the ‘displayOnMATLAB’ function on this object.
void operator()(ArgumentList outputs, ArgumentList inputs) {
const CharArray name = inputs[0];
const TypedArray<double> number = inputs[1];
stream << "Here is the name/value pair that you entered." << std::endl;
displayOnMATLAB(stream);
stream << name.toAscii() << ": " << double(number[0]) << std::endl;
displayOnMATLAB(stream);
}
  • Create the ‘displayOnMATLAB’ method inside the class. This method leverages the ‘feval’ function in conjunction with ‘fprintf’ to print the contents of the ‘ostringstream’ object onto the MATLAB Command line.
void displayOnMATLAB(std::ostringstream& stream) {
matlabPtr->feval(u"fprintf", 0,std::vector<Array>({ factory.createScalar(stream.str()) }));
stream.str("");
}
The code is saved under the name ‘streamOutput.cpp’. Below is a screenshot illustrating the execution of the code along with the anticipated output.
Hope it helps!
  1 Comment
Trevor
Trevor on 30 May 2024
Edited: Trevor on 30 May 2024
Hi Tejas,
Thanks for your response. This seems like the common mex approach where the filename of the cpp file (streamOutput.cpp) turns into a function that can be called within matlab.
I am looking at having messages print from functions that were built with the clib interface generator. (binding custom c++ code) which seems like I should be able to just connect to a matlab instance, use streams, and call feval etc.

Sign in to comment.

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!