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

10 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

Pramil
Pramil el 17 de Oct. de 2024
Editada: Pramil el 17 de Oct. de 2024
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 comentarios
Pramil
Pramil el 18 de Oct. de 2024
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 el 18 de Oct. de 2024
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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Configure and View Diagnostics en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by