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

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!

 Accepted Answer

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 Comments

Thank you for your detailed answer. I have attempted to use mexPrintf(), but am still not seeing output. I have simplified my code to show the behavior.
I start with a new Simulink model. I have a C Function block set up like:
logwriter.cpp consists of:
#include "logwriter.h"
#include "mex.h"
#include <string>
EXTERNC int write()
{
return mexPrintf("This is a test\n");
}
I also have "logwriter.h" included under the Headers section of the C Function Block. The header consists of:
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
EXTERNC int write();
I can run this simulation in Simulink, and it shows that mexPrintf() thinks it printed 15 characters, but I don't see anything in either the Diagnostic Viewer or the Matlab Command window.
Any pointers on what might be going wrong would be appriciated! Thanks.
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.
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!

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Supported Hardware in Help Center and File Exchange

Products

Release

R2024b

Asked:

on 14 Oct 2024

Commented:

on 18 Oct 2024

Community Treasure Hunt

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

Start Hunting!