MATLAB Functions Help Desk

fprintf

Purpose

Write formatted data to file

Syntax

Description

count = 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.

Argument fid is an integer file identifier obtained from fopen. (It may also be 1 for standard output (the screen) or 2 for standard error. See fopen for more information.) Omitting fid from fprintf's argument list causes output to appear on the screen, and is the same as writing to standard output (fid = 1)

fprintf(format,A,...) writes to standard output--the screen.

The format string specifies notation, alignment, significant digits, field width, and other aspects of output format. It can contain ordinary alphanumeric characters; along with escape characters, conversion specifiers, and other characters, organized as shown below:

For more information see "Tables" and "References".

Remarks

The fprintf function behaves like its ANSI C language fprintf() namesake with certain exceptions and extensions. These include:

  1. The following non-standard subtype specifiers are supported for conversion specifiers %o, %u, %x, and %X.

    t

    The underlying C data type is a float rather than an unsigned integer.

    b

    The underlying C data type is a double rather than an unsigned integer.


    For example, to print a double-precision value in hexadecimal, use a format like '%bx'.

  2. The fprintf function is vectorized for the case when input matrix A is nonscalar. The format string is cycled through the elements of A (columnwise) until all the elements are used up. It is then cycled in a similar manner, without reinitializing, through any additional matrix arguments.

Tables

The following tables describe the non-alphanumeric characters found in format specification strings.

Escape Characters

Character

Description

\n

New line

\t

Horizontal tab

\b

Backspace

\r

Carriage return

\f

Form feed

\\

Backslash

\'' or ''

Single quotation mark

%%

Percent character

Conversion characters specify the notation of the output.

Conversion Specifiers

Specifier

Description

%c

Single character

%d

Decimal notation (signed)

%e

Exponential notation (using a lowercase e as in 3.1415e+00)

%E

Exponential notation (using an uppercase E as in 3.1415E+00)

%f

Fixed-point notation

%g

The more compact of %e or %f, as defined in [2]. Insignificant zeros do not print.

%G

Same as %g, but using an uppercase E

%o

Octal notation (unsigned)

%s

String of characters

%u

Decimal notation (unsigned)

%x

Hexadecimal notation (using lowercase letters a-f)

%X

Hexadecimal notation (using uppercase letters A-F)

Other characters can be inserted into the conversion specifier between the % and the conversion character.

Other Characters

Character

Description

Example

A minus sign (-)

Left-justifies the converted argument in its field.

%-5.2d

A plus sign (+)

Always prints a sign character (+ or -).

%+5.2d

Zero (0)

Pad with zeros rather than spaces.

%05.2d

Digits (field width)

A digit string specifying the minimum number of digits to be printed.

%6f

Digits (precision)

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

%6.2f

For more information about format strings, refer to the printf() and fprintf() routines in the documents listed in "References".

Examples

The statements

create a text file called exp.txt containing a short table of the exponential function:

The command

displays a line on the screen:

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

displays on the screen:

The commands

display the lines:

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

See Also

fclose      Close one or more open files

ferror      Query MATLAB about errors in file input or output

fopen       Open a file or obtain information about open files

fscanf      Read formatted data from file

fseek       Set file position indicator

ftell       Get file position indicator

References

[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 | Help Desk | Next ]