How can I write to either the MATLAB Command Window or the Simulink Diagnostic Viewer from code called by a C Function block?

조회 수: 10 (최근 30일)
I have a C Function Block that calls a C wrapper of a long C++ method defined in an external file. I would like to periodically write messages from that C++ code and have the messages appear at runtime either in the MATLAB command window or the Simulink Diagnostic Viewer. I have tried redirecting stdout with
if (AllocConsole() == 0) {
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
But I still see nothing from either std::cout or fprintf calls. Any help would be appriciated!

채택된 답변

Pramil
Pramil 2024년 10월 17일
편집: Pramil 2024년 10월 17일
Hello Jeff,
You can use the "mexPrintf" function for printing messages during runtime on Simulink Diagnostic Viewer from C++ code. Here is a link to the documentation for the same:
Here is how you can achieve this:
  • Include the header files "mex.h" in the C++ code.
  • Declare a "print_val" or any other function for printing in the header file with input set to "const char*".
  • Implement the "print_val" function like below:
void className::print_val(const char* msg)
{
mexPrintf(msg);
}
  • Call the '"print_val" function in the C++ code with the message for printing the messages at runtime.
Hope it helps.
  댓글 수: 3
Pramil
Pramil 2024년 10월 18일
I tried your code and was able to get output in Simulink Diagnostic Viewer, I am attaching my model in this comment so you can refer the same.
Jeff
Jeff 2024년 10월 18일
Ok, with your model I was able to figure out the problem.
Your model, which uses Model Settings->Simulation Target->Custom Code to include "logwriter.h" and "logwriter.cpp", works just fine to display information in the Diagnostic Viewer.
If I move the links to the external "logwriter.cpp" and "logwriter.h" from Simulation Target->Custom Code to the C Function Block Paramaters->Simulation Custom Code (as I had in my model, above), the code compiles just fine, but no output is displayed in the Diagnostic Viewer.
Interesting result, but I can work with this. Thanks for the help in debugging it!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Configure and View Diagnostics에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by