MATLAB Function Reference | ![]() ![]() |
Syntax
[A,count] = fread(fid,size,precision
) [A,count] = fread(fid,size,precision
,
skip)
Description
[A,count] = fread(fid,size,precision)
reads binary data from the specified file and writes it into matrix A
. Optional output argument count
returns the number of elements successfully read. fid
is an integer file identifier obtained from fopen
.
size
is an optional argument that determines how much data is read. If size
is not specified, fread
reads to the end of the file and the file pointer is at the end of the file (see feof
for details). Valid options are:
precision
is a string that specifies the format of the data to be read. It commonly contains a datatype specifier such as int
or float
, followed by an integer giving the size in bits. Any of the strings in the following table, either the MATLAB version or their C or Fortran equivalent, may be used. If precision is not specified, the default 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 |
|
- |
Signed integer; N bits (1 ![]() N ![]() 64 ) |
|
- |
Unsigned integer; N bits (1 ![]() N ![]() 64 ) |
By default, numeric values are returned in class 'double'
arrays. To return numeric values stored in classes other thatn 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 reuiqred 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 may be used:
*source
source=>source
This table shows some example precision format strings.
[A,count] = fread(fid,size,
includes an optional precision
,
skip)
skip
argument that specifies the number of bytes to skip after each precision
value is read. With skip
is used, the precision
string may contain a positive integer repetition factor of the form 'N*'
which prepends 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), does a skip of input specified by the skip
argument, reads in another block of values, does a skip of input, and so on, until size
number of values have beenread. If precision
is a bit format like 'bitN'
or 'ubitN'
, skip
is specified in bits. Use the repetition factor with the skip
argument to extract data in noncontiguous fields from fixed length records.
If fread
reaches the end of the file and the current input stream does not contain enough bits to write out a complete matrix element of the specified precision, fread
pads the last byte or element with zero bits until the full value is obtained. If an error occurs, reading is done up to the last full value.
Examples
type fread.m
displays the complete M-file containing this fread help entry. To simulate this command using fread, enter the following:
fid = fopen('fread.m','r'); F = fread(fid); s = char(F')
In the example, the fread
command assumes the default size, inf
, and the default precision, 'uchar'
. fread
reads the entire file, converting the unsigned characters into a column vector of class 'double'
(double precision floating point). To display the result as readable text, the 'double'
column vector is transposed to a row vector and converted to class 'char'
using the char
function.
s = fread(fid,120,'40*uchar=>uchar',8);
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 will be done before the command is finished. If the last block read is not a full block then fread
will not finish with a skip.
See fopen
for informationabout reading Big and Little Endian files.
See Also
fclose
, ferror
, fopen
, fprintf
, fread
, fscanf
, fseek
, ftell
, fwrite
![]() | frameedit | fread (serial) | ![]() |