Why do "fprintf" and "disp" not show in the Command Window when called from Simulink callbacks? Why and when does this happen, and how is the Diagnostic Viewer involved?

48 views (last 30 days)
Since migrating from R2012a to R2015b, I noticed that "fprintf" and "disp" do not show in the MATLAB Command Window when called from Simulink callbacks.
Is this a bug? I did not find any description of this feature in the documentation and bug reports.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Sep 2021
Edited: MathWorks Support Team on 6 Oct 2021
This output redirection is occurring due to the Diagnostic Viewer, which was introduced in R2014a.
In general, standard output (stdout) from "fprintf" and "disp" is redirected to the Diagnostic Viewer when the corresponding callback is invoked via the GUI, and is directed to the Command Window when the callback is invoked via the command-line. Output to standard error (stderr) from "fprintf" is always shown in the Command Window.
For example, if a model's "StartFcn" calls "disp":
  • If the Simulation is started using the GUI (the Play button), then the output is directed to the Diagnostic Viewer.
  • If the Simulation is started using the "sim" command, the output is directed to the Command Window.
Some of the exceptions in this case:
  • Callbacks invoked upon opening a GUI version of the block diagram, either by "uiopen" or "open_system", always have "stdout" redirected to the Diagnostic Viewer. Callbacks invoked by "load_system" are output to the Command Window.
  • Callbacks for 'OpenFcn' for blocks are output to the Command Window.
These are the possible workarounds:
1) Attached is an example, "sldiagviewer_io_redirection_timer.zip", where you may see this in action by following these steps:
i. Download and extract the archive into the same directory.
ii. Execute the "example" script and follow the instructions.
iii. Note the workaround using "timer_printf" to call "printf" in a separate environment, ultimately placing the output in the Command Window.
2) Output to stderr with "fprintf":
fprintf(2, 'Test message.\n');
Please note that in this case, the Command Window will the color this text as red.
3) Use the 'sim' command in a MATLAB script to display all the errors and build information on Command Window. 
4) If it is appropriate for your use case, you may log simulation warnings and errors and build information to a TXT file using "sldiagviewer.diary". For more information about this function, please refer to the following documentation: 

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!