How do I print to stderr?

조회 수: 40 (최근 30일)
Michael
Michael 2012년 5월 24일
댓글: Gabriele Bellomia 2022년 5월 4일
I'd like to print to standard error. I'm sure there is a simple solution, but the "display" documentation didn't specify, and I didn't find a clear answer when searching this forum. How do you do it in Matlab?

답변 (2개)

Michael
Michael 2012년 5월 24일
Sorry, the answer is to use fprintf(2,"stuff"). I can't tell if there is a way to get display to print to stdout

Walter Roberson
Walter Roberson 2012년 5월 24일
Unfortunately MATLAB does not support stdout or stderr. The closest it has is writing to file 1 (sort of like stdout) or file 2 (sort of like stderr). Both file 1 and file 2 will go to wherever the command output is going, such as the command window. Neither of them are connected to the Unix stderr stream. If the session was started with -nodisplay (or equivalent) so that there is no command window, then both of them are connected to stdout -- but if the command window is active, then neither of them are connected to stdout.
The only difference I have been able to infer in MATLAB between writing to file 1 and file 2, is that it appears from one of the documentation examples (somewhere) that file 2 might not be buffered, that output to it might potentially get mixed in with output that is already in progress.
  댓글 수: 2
Michael
Michael 2012년 5월 24일
Thank you. I will add this to the list of reasons to use Pylab ( <a href="http://www.scipy.org/PyLab">link</a> ). This makes monitoring of remote Matlab jobs difficult, if errors cannot be separated from valid output.
Gabriele Bellomia
Gabriele Bellomia 2022년 5월 4일
Just to clarify for the 2022 reader: I don't know if the behavior has changed and when this would have happened but I can confirm that with MATLAB > R2020b 'fprinting' to '2' totally works for remote hpc workflows. In particular I succesfully tested the behavior on a SLURM-managed CentOS machine, and I consistently find all the fprintf(2,...) outputs in the stderr logs built by SLURM. :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by