MATLAB Function Reference Previous page   Next Page

Read binary data from file



A = fread(fid) reads data in binary format from the file specified by fid into matrix A. Open the file using fopen before calling fread. The fid argument is the integer file identifier obtained from the fopen operation. MATLAB reads the file from beginning to end, and then positions the file pointer at the end of the file (see feof for details).

A = fread(fid, count) reads the number of elements specified by count. At the end of the fread, MATLAB sets the file pointer to the next byte to be read. A subsequent fread will begin at the location of the file pointer. See Specifying the Number of Elements, below.

A = fread(fid, count, precision) reads the file according to the data format specified by the string precision. This argument commonly contains a data type specifier such as int or float, followed by an integer giving the size in bits. See Specifying Precision and Specifying Output Precision, below.

A = fread(fid, count, precision, skip) includes an optional skip argument that specifies the number of bytes to skip after each precision value is read. If precision specifies a bit format like 'bitN' or 'ubitN', the skip argument is interpreted as the number of bits to skip. See Specifying a Skip Value, below.

A = fread(fid, count, precision, skip, machineformat) treats the data read as having a format given by machineformat. You can obtain the machineformat argument from the output of the fopen function. See Specifying Machine Format, below.

[A, count] = fread(...) returns the data read from the file in A, and the number of elements successfully read in count.

Specifying the Number of Elements

Valid options for count are

Reads n elements into a column vector.
Reads to the end of the file, resulting in a column vector containing the same number of elements as are in the file. If using inf results in an "out of memory" error, specify a numeric count value.
Reads enough elements to fill an m-by-n matrix, filling in elements in column order, padding with zeros if the file is too small to fill the matrix. n can be specified as inf, but m cannot.

Specifying Precision

Any of the strings in the following table, either the MATLAB version or their C or Fortran equivalent, can be used for precision. If precision is not specified, MATLAB uses the default, which is 'uchar'.

C or Fortran
'signed char'
Signed character; 8 bits
'unsigned char'
Unsigned character; 8 bits
Integer; 8 bits
Integer; 16 bits
Integer; 32 bits
Integer; 64 bits
Unsigned integer; 8 bits
Unsigned integer; 16 bits
Unsigned integer; 32 bits
Unsigned integer; 64 bits
Floating-point; 32 bits
Floating-point; 64 bits
Floating-point; 64 bits

The following platform-dependent formats are also supported, but they are not guaranteed to be the same size on all platforms.

C or Fortran
Character; 8 bits
Integer; 16 bits
Integer; 32 bits
Integer; 32 or 64 bits
'unsigned short'
Unsigned integer; 16 bits
'unsigned int'
Unsigned integer; 32 bits
'unsigned long'
Unsigned integer; 32 or 64 bits
Floating-point; 32 bits

The following formats map to an input stream of bits rather than bytes.

C or Fortran
Signed integer; N bits (1 N 64)
Unsigned integer; N bits (1 N 64)

Specifying Output Precision

By default, numeric values are returned in class double arrays. To return numeric values stored in classes other than double, create your precision argument by first specifying your source format, then following it with the characters "=>", and finally specifying your destination format. You are not required to use the exact name of a MATLAB class type for destination. (See class for details). fread translates the name to the most appropriate MATLAB class type. If the source and destination formats are the same, the following shorthand notation can be used.

which means

For example, '*uint16' is the same as 'uint16=>uint16'.

This table shows some example precision format strings.

Read in unsigned 8-bit integers and save them in an unsigned 8-bit integer array.
Shorthand version of the above.
Read in signed 4-bit integers packed in bytes and save them in a signed 8-bit array. Each 4-bit integer becomes an 8-bit integer.
Read in doubles, convert, and save as a 32-bit floating-point array.

Specifying a Skip Value

When skip is used, the precision string can contain a positive integer repetition factor of the form 'N*', which prefixes the source format specification, such as '40*uchar'.

When skip is specified, fread reads in, at most, a repetition factor number of values (default is 1), skips the amount of input specified by the skip argument, reads in another block of values, again skips input, and so on, until count number of values have been read. If a skip argument is not specified, the repetition factor is ignored. Use the repetition factor with the skip argument to extract data in noncontiguous fields from fixed-length records.

Specifying Machine Format

machineformat is one of the following strings:

'cray' or 'c'
Cray floating point with big-endian byte ordering
'ieee-be' or 'b'
IEEE floating point with big-endian byte ordering
'ieee-le' or 'l'
IEEE floating point with little-endian byte ordering
'ieee-be.l64' or 's'
IEEE floating point with big-endian byte ordering and 64-bit long data type
'ieee-le.l64' or 'a'
IEEE floating point with little-endian byte ordering and 64-bit long data type
'native' or 'n'
Numeric format of the machine on which MATLAB is running (the default)
'vaxd' or 'd'
VAX D floating point and VAX ordering
'vaxg' or 'g'
VAX G floating point and VAX ordering


When reading character strings from files, pass the output of fread to the MATLAB native2unicode function to ensure that characters display correctly:

If the input stream is bytes and fread reaches the end of file (see feof) in the middle of reading the number of bytes required for an element, the partial result is ignored. However, if the input stream is bits, then the partial result is returned as the last value. If an error occurs before reaching the end of file, only full elements read up to that point are used.


Example 1

The file alphabet.txt contains the 26 letters of the English alphabet, all capitalized. Open the file for read access with fopen, and read the first five elements into output c. Because a precision has not been specified, MATLAB uses the default precision of uchar, and the output is numeric:

This time, specify that you want each element read as an unsigned 8-bit integer and output as a character. (Using a precision of 'char=>char' or '*char' will produce the same result):

When you leave out the optional count argument, MATLAB reads the file to the end, A through Z:

The fopen function positions the file pointer at the start of the file. So the first fread in this example reads the first five elements in the file, and then repositions the file pointer at the beginning of the next element. For this reason, the next fread picks up where the previous fread left off, at the character F.

Skip two elements between each read by specifying a skip argument of 2:

Example 2

This command displays the complete M-file containing this fread help entry:

To simulate this command using fread, enter the following:

In the example, the fread command assumes the default size, 'inf', and precision '*uchar' (the same as 'char=>char'). fread reads the entire file. To display the result as readable text, the column vector is transposed to a row vector.

Example 3

As another example,

reads in 120 characters in blocks of 40, each separated by 8 characters. Note that the class type of s is 'uint8' since it is the appropriate class corresponding to the destination format 'uchar'. Also, since 40 evenly divides 120, the last block read is a full block, which means that a final skip is done before the command is finished. If the last block read is not a full block, then fread does not finish with a skip.

See fopen for information about reading big and little-endian files.

Example 4

Invoke the fopen function with just an fid input argument to obtain the machine format for the file. You can see that this file was written in IEEE floating point with little-endian byte ordering ('ieee-le') format:

Use the MATLAB format function (not related to the machine format type) to have MATLAB display output using hexadecimal:

Now use the machineformat input with fread to read the data from the file using the same format:

Change the machine format to IEEE floating point with big-endian byte ordering ('ieee-be') and verify that you get different results:

See Also

fgetl, fscanf, fwrite, fprintf, fopen, fclose, fseek, ftell, feof

Previous page  frameedit freqspace Next page

© 1994-2005 The MathWorks, Inc.