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

8 次查看(过去 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