MATLAB Function Reference |
Syntax
A = fread(fid) A = fread(fid, count) A = fread(fid, count, precision) A = fread(fid, count, precision, skip) A = fread(fid, count, precision, skip, machineformat) [A, count] = fread(...)
Description
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).
Note
fread is intended primarily for binary data. When reading text files, use the fgetl function.
|
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.
Note
In the following syntaxes, the count and skip arguments are optional. For example, fread(fid, precision) is a valid syntax.
|
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
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'
.
The following platform-dependent formats are also supported, but they are not guaranteed to be the same size on all platforms.
The following formats map to an input stream of bits rather than bytes.
MATLAB |
C or Fortran |
Interpretation |
'bitN' |
- |
Signed integer; N bits (1 N 64 ) |
'ubitN' |
- |
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.
For example, '*uint16'
is the same as 'uint16=>uint16'
.
This table shows some example precision format strings.
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:
Remarks
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
:
fid = fopen('alphabet.txt', 'r'); c = fread(fid, '*char')' c = ABCDEFGHIJKLMNOPQRSTUVWXYZ fclose(fid);
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
.
fid = fopen('alphabet.txt', 'r'); c1 = fread(fid, 5, '*char'); c2 = fread(fid, 8, '*char'); c3 = fread(fid, 5, '*char'); fclose(fid); sprintf('%c', c1, ' * ', c2, ' * ', c3) ans = ABCDE * FGHIJKLM * NOPQR
Skip two elements between each read by specifying a skip
argument of 2
:
fid = fopen('alphabet.txt', 'r'); c = fread(fid, 'char', 2); % Skip 2 bytes per read fclose(fid); sprintf('%c', c) ans = ADGJMPSVY
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
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:
x = fread(fid, 6, 'uint64', 'ieee-le') x = 4260800000002000 0000000000000000 4282000000180000 0000000000000000 42ca5e0000258000 42f0000464d45200 fclose(fid);
Change the machine format to IEEE floating point with big-endian byte ordering ('ieee-be'
) and verify that you get different results:
fid = fopen('A1.dat', 'r'); x = fread(fid, 6, 'uint64', 'ieee-be') x = 4370000008400000 0000000000000000 4308000200100000 0000000000000000 4352c0002f0d0000 43c022a6a3000000 fclose(fid);
See Also
fgetl
, fscanf
, fwrite
, fprintf
, fopen
, fclose
, fseek
, ftell
, feof
frameedit | freqspace |
© 1994-2005 The MathWorks, Inc.