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: