MATLAB Function Reference Previous page   Next Page

Read formatted data from file



A = fscanf(fid, format) reads all the data from the file specified by fid, converts it according to the specified format string, and returns it in matrix A. Argument fid is an integer file identifier obtained from fopen. format is a string specifying the format of the data to be read. See "Remarks" for details.

[A,count] = fscanf(fid, format, size) reads the amount of data specified by size, converts it according to the specified format string, and returns it along with a count of elements successfully read. size is an argument that determines how much data is read. Valid options are

Read n elements into a column vector.
Read to the end of the file, resulting in a column vector containing the same number of elements as are in the file.
Read enough elements to fill an m-by-n matrix, filling the matrix in column order. n can be specified as inf, but m cannot.

fscanf differs from its C language namesakes scanf() and fscanf() in an important respect -- it is vectorized in order to return a matrix argument. The format string is cycled through the file until an end-of-file is reached or the amount of data specified by size is read in.


When MATLAB reads a specified file, it attempts to match the data in the file to the format string. If a match occurs, the data is written into the matrix in column order. If a partial match occurs, only the matching data is written to the matrix, and the read operation stops.

The format string consists of ordinary characters and/or conversion specifications. Conversion specifications indicate the type of data to be matched and involve the character %, optional width fields, and conversion characters, organized as shown below.

Add one or more of these characters between the % and the conversion character:

An asterisk (*)
Skip over the matched value. If %*d, then the value that matches d is ignored and is not stored.
A digit string
Maximum field width. For example, %10d.
A letter
The size of the receiving object, for example, h for short, as in %hd for a short integer, or l for long, as in %ld for a long integer, or %lg for a double floating-point number.

Valid conversion characters are

Sequence of characters; number specified by field width
Base 10 integers
%e, %f, %g
Floating-point numbers
Defaults to base 10 integers. Data starting with 0 is read as base 8. Data starting with 0x or 0X is read as base 16.
Signed octal integer
A series of non-white-space characters
Signed decimal integer
Signed hexadecimal integer
Sequence of characters (scanlist)

If %s is used, an element read can use several MATLAB matrix elements, each holding one character. Use %c to read space characters or %s to skip all white space.

Mixing character and numeric conversion specifications causes the resulting matrix to be numeric and any characters read to appear as their ASCII values, one character per MATLAB matrix element.

For more information about format strings, refer to the scanf() and fscanf() routines in a C language reference manual.


The example in fprintf generates an ASCII text file called exp.txt that looks like

Read this ASCII file back into a two-column MATLAB matrix:

See Also

fgetl, fgets, fread, fprintf, fscanf, input, sscanf, textread

Previous page  frewind fseek Next page

© 1994-2005 The MathWorks, Inc.