difference between sprintf and fprintf
Mostra commenti meno recenti
I've read many answers regarding this question, but I still do not get the gist of it. So what if sprintf writes to a screen, while fprintf writes to a file? How does it matter/make a difference?
Please try to explain in layman's term because I'm only a beginner.
Thanks!!
Risposta accettata
Più risposte (1)
Walter Roberson
il 16 Set 2011
3 voti
Yes, both of them format data using the same formatting rules. sprintf() returns the formatted data as a string, which the user can store or further manipulate or display as appropriate. fprintf() writes the formatted data to whichever file or device (such as a serial port) it is connected to.
You may have also seen reference to fwrite(). fprintf(fid,...) is (for these purposes) the same as fwrite(fid,sprintf(...)) but fprintf() does not (theoretically) need to build the entire string before writing it out to the connected device. fprintf() could be thought of as a convenience for the combination of sprintf and fwrite, but it is a convenience that is used often enough to be worth having.
(There is a small difference between fprintf() and sprintf() if the destination is a serial port, but it is unimportant at the moment.)
2 Commenti
Seetha Rama Raju Sanapala
il 11 Mag 2015
fprintf without the fID, sends the string to the screen only. If fprintf can send to a file or screen or a device, then why separately have sprintf for screen. I also did not understand the difference. If someone can exemplify the difference it would be helpful.
Walter Roberson
il 11 Mag 2015
fprintf() sends the string to an output device (screen, file). In an output context, such as assigning to a variable, it returns the number of bytes written. You could store that byte count in a variable or use that byte count in an expression. If you do not have an output context then MATLAB does not return anything. If you do have an output context then as usual for MATLAB if you do not have a semi-colon on the end of the expression, the last returned expression will be displayed using the "ans =" format.
sprintf() does not in itself send the string to an output device. sprintf() always returns a value, which is the formatted string. You can store the string in a variable for later use. If you do not have a semi-colon on the end of the line, then as usual for MATLAB it will helpfully display the last returne value (in this case the formatted string) in the "ans =" format.
Compare
fprintf('hello\n')
fprintf('hello\n');
x = fprintf('hello\n')
disp(x)
sprintf('hello\n')
sprintf('hello\n');
y = sprintf('hello\n')
disp(y)
Note that the sprintf('hello\n'); produced no output, because the return from sprintf() is the string itself without any device output and the semi-colon says not to display the return value.
Categorie
Scopri di più su Environment and Settings in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!