| Contents | Index |
fprintf(fileID,formatSpec,A1,...,An)
fprintf(formatSpec,A1,...,An)
nbytes = fprintf(fileID,formatSpec,A1,...,An)
fprintf(fileID,formatSpec,A1,...,An) applies the formatSpec to all elements of arrays A1,...An in column order, and writes the data to a text file. fprintf uses the encoding scheme specified in the call to fopen.
fprintf(formatSpec,A1,...,An) formats data and displays the results on the screen.
nbytes = fprintf(fileID,formatSpec,A1,...,An) returns the number of bytes that fprintf writes.
Format specifiers for the reading functions sscanf and fscanf differ from the formats for the writing functions sprintf and fprintf. The reading functions do not support a precision field. The width field specifies a minimum for writing but a maximum for reading.
fileID | One of the following:
Default: 1 (the screen) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formatSpec |
String that describes the format of the output fields. Can include combinations of the following:
Conversion characters and optional operators appear in the following order (includes spaces for clarity):
The following table lists the available conversion characters and subtypes.
Additional operators include:
The following limitations apply to conversions:
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
A1,...,An |
Numeric or character arrays. |
nbytes |
Number of bytes that fprintf writes. |
Print multiple numeric values and literal text to the screen.
A1 = [9.9, 9900]; A2 = [8.8, 7.7 ; ... 8800, 7700]; formatSpec = 'X is %4.2f meters or %8.3f mm\n'; fprintf(formatSpec,A1,A2)
X is 9.90 meters or 9900.000 mm X is 8.80 meters or 8800.000 mm X is 7.70 meters or 7700.000 mm
Explicitly convert double-precision values with fractions to integer values.
a = [1.02, 3.04, 5.06];
fprintf('%d\n',round(a));1 3 5
Write a short table of the exponential function to a text file called exp.txt.
x = 0:.1:1; A = [x; exp(x)]; fileID = fopen('exp.txt','w'); fprintf(fileID,'%6s %12s\n','x','exp(x)'); fprintf(fileID,'%6.2f %12.8f\n',A); fclose(fileID);
The first call to fprintf prints header text x and exp(x), and the second call prints the values from variable A.
If you plan to read the file with Microsoft Notepad, use '\r\n' instead of '\n' to move to a new line. For example, replace the calls to fprintf with the following:
fprintf(fileID,'%6s %12s\r\n','x','exp(x)'); fprintf(fileID,'%6.2f %12.8f\r\n',A);
MATLAB import functions, all UNIX applications, and Microsoft Word and WordPad recognize '\n' as a newline indicator.
View the contents of the file with the type command.
type exp.txtDisplay a hyperlink (The MathWorks Web Site) on the screen.
site = 'http://www.mathworks.com'; title = 'The MathWorks Web Site'; fprintf('<a href = "%s">%s</a>\n',site,title)
[1] Kernighan, B. W., and D. M. Ritchie, The C Programming Language, Second Edition, Prentice-Hall, Inc., 1988.
[2] ANSI specification X3.159-1989: "Programming Language C," ANSI, 1430 Broadway, New York, NY 10018.
disp | fclose | ferror | fopen | fread | fscanf | fseek | ftell | fwrite | sprintf

Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |