MATLAB Function Reference Previous page   Next Page

Write formatted data to file



[count, errmsg] = fprintf(fid, format, A, ...) formats the data in the real part of matrix A (and in any additional matrix arguments) under control of the specified format string, and writes it to the file associated with file identifier fid. fprintf returns a count of the number of bytes written. errmsg is an optional output argument that returns an error message string if an error occurred, or an empty string if an error did not occur.

Argument fid is an integer file identifier obtained from fopen. (It can also be 1 for standard output (the screen) or 2 for standard error. See fopen for more information.) Omitting fid causes output to appear on the screen.

Format String

The format argument is a string containing C language conversion specifications. A conversion specification controls the notation, alignment, significant digits, field width, and other aspects of output format. The format string can contain escape characters to represent nonprinting characters such as newline characters and tabs.

Conversion specifications begin with the % character and contain these optional and required elements:

You specify these elements in the following order:


You can control the alignment of the output using any of these optional flags.

Minus sign (-)
Left-justifies the converted argument in its field
Plus sign (+)
Always prints a sign character (+ or -)
Space character
Inserts a space before the value
% 5.2d
Zero (0)
Pads with zeros rather than spaces

Field Width and Precision Specifications

You can control the width and precision of the output by including these options in the format string.

Field width
A digit string specifying the minimum number of digits to be printed
A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point

Conversion Characters

Conversion characters specify the notation of the output.

Single character
Decimal notation (signed)
Exponential notation (using a lowercase e as in 3.1415e+00)
Exponential notation (using an uppercase E as in 3.1415E+00)
Fixed-point notation
The more compact of %e or %f, as defined in [2]. Insignificant zeros do not print.
Same as %g, but using an uppercase E
Decimal notation (signed)
Octal notation (unsigned)
String of characters
Decimal notation (unsigned)
Hexadecimal notation (using lowercase letters a-f)
Hexadecimal notation (using uppercase letters A-F)

Conversion characters %o, %u, %x, and %X support subtype specifiers. See Remarks for more information.

Escape Characters

This table lists the escape character sequences you use to specify nonprinting characters in a format specification.

Form feed
New line
Carriage return
Horizontal tab
\'' or ''

(two single quotes)

Single quotation mark
Percent character


When writing text to a file on Windows, it is recommended that you open the file in write-text mode (e.g., fopen(file_id, 'wt')). This ensures that lines in the file are terminated in such a way as to be compatible with all applications that might use the file.

The fprintf function behaves like its ANSI C language namesake with these exceptions and extensions:

Conversion Character
%e or %E
%g or %G


Example 1

Create a text file called exp.txt containing a short table of the exponential function. (On Windows platforms, it is recommended that you use fopen with the mode set to 'wt' to open a text file for writing.)

Now examine the contents of exp.txt:

Example 2

The command

displays a line on the screen:

Example 3

To insert a single quotation mark in a string, use two single quotation marks together. For example,

displays on the screen

Example 4

The commands

display the lines

Example 5

Explicitly convert MATLAB double-precision variables to integer values for use with an integer conversion specifier. For instance, to convert signed 32-bit data to hexadecimal format,

See Also

fclose, ferror, fopen, fread, fscanf, fseek, ftell, fwrite, disp


[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.

Previous page  fplot frame2im Next page

© 1994-2005 The MathWorks, Inc.