As the title reveals, when I use "fprintf" with a semicolon at the end, the argument in fprintf command seems to appear normally in command window. Is it just because fprintf command is designed to ignore in purpose the semicolon or something else?
I just noticed that "disp" behaves the same way as "fprintf". I know that I could use a "%" at the beginning of "fprintf". I just want to know about this behaviour.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 26 Feb. 2015

0 Stimmen

I don't understand the problem. fprintf() does not return any value unless you're using a file handle. If you use no file handle, or a file handle of 1 (which is the command window), then it will just do it's job of printing your stuff to the command window but not return any additional value. It will not behave any differently if there is or is not a semicolon at the end of the line, just as expected and designed. So, yeah it ignores it - just like it should. Do you expect differently for some reason?
Now if you did use a file handle and printed to a disk file, then using a semicolon would not print anything to the command window, but leaving off the semicolon would print (in the command window) the number of bytes written to the disk file. In this case having or not having a semicolon does display things differently in the command window, and the semicolon is NOT ignored, again just as expected.
I don't see any confusion or discrepancy in the way it works. I hope that explains it better.

3 Kommentare

Guillaume
Guillaume am 26 Feb. 2015
Bearbeitet: Guillaume am 26 Feb. 2015
I think it's a misunderstanding on the part of Th Sdr about the function of the semicolon at the end of a line.
A semicolon does not suppress output to the command line. It only suppresses output of the return value to the command line. As fprintf does not have any return value, semicolon has nothing to suppress.
However, fprintf's function is to display output to the console (when not given a file handle > 2), so it's always going to do that, just like a plot function is always going to plot something.
Th Sdr
Th Sdr am 26 Feb. 2015
Bearbeitet: Th Sdr am 26 Feb. 2015
Thank you both (for the answer and the comment). I was not aware of the exact function of "fprintf" command and as it seems I was not fully aware of the function of the semicolon.
Stephen23
Stephen23 am 23 Mai 2020
"As fprintf does not have any return value"
Not quite: it actually returns the numbers of bytes printed.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by