필터 지우기
필터 지우기

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

조회 수: 8 (최근 30일)
Trevor
Trevor 2024년 4월 10일
편집: Trevor 2024년 5월 30일
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

답변 (1개)

Tejas
Tejas 2024년 4월 24일
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
Trevor
Trevor 2024년 5월 30일
편집: Trevor 2024년 5월 30일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Deploy to C++ Applications Using MATLAB Data API (C++11)에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by