| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
fprintf(fileID, format, A,
...)
fprintf(format, A,
...)
count = fprintf(...)
fprintf(fileID, format, A, ...) applies the format to array A and any additional array arguments in column order, and writes the data to a text file. fprintf uses the encoding scheme specified in the call to fopen.
fprintf(format, A, ...) formats data and displays the results on the screen.
count = fprintf(...) returns the number of bytes that fprintf writes.
fileID |
One of the following:
Default: 1 (the screen) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
format |
String in single quotation marks 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:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
A |
Numeric or character array. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Print multiple values and literal text to the screen:
B = [8.8 7.7 ; ...
8800 7700];
fprintf('X is %4.2f meters or %8.3f mm\n', 9.9, 9900, B)MATLAB displays:
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, and print to the screen:
a = [1.02 3.04 5.06];
fprintf('%d\n', round(a));
Write a short table of the exponential function to a text file called exp.txt:
x = 0:.1:1;
y = [x; exp(x)];
% open the file with write permission
fid = fopen('exp.txt', 'w');
fprintf(fid, '%6.2f %12.8f\n', y);
fclose(fid);
% view the contents of the file
type exp.txtMATLAB import functions, all UNIX applications, and Microsoft Word and WordPad recognize '\n' as a newline indicator. However, if you plan to read the file with Microsoft Notepad, use '\r\n' to move to a new line when writing. For example, replace the previous call to fprintf with the following:
fprintf(fid, '%6.2f %12.8f\r\n', y);
On a Windows system, convert PC-style exponential notation (three digits in the exponent) to UNIX-style notation (two digits), and print data to a file:
a = [0.06 0.1 5 300]
% use sprintf to convert the numeric data to text, using %e
a_str = sprintf('%e\t',a);
% use strrep to replace exponent prefix with shorter version
a_str = strrep(a_str,'e+0','e+');
a_str = strrep(a_str,'e-0','e-');
% call fprintf to print the updated text strings
fid = fopen('newfile.txt','w');
fprintf(fid, '%s', a_str);
fclose(fid);
% view the contents of the file
type newfile.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 | fwrite | sprintf
![]() | fplot | fprintf (serial) | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |