Syntax
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
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 than double
, create your precision argument by first specifying your source format, and 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.
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. If precision
specifies a bit format, like 'bitN'
or 'ubitN'
, the skip
argument is interpreted as the number of bits to skip.
When 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), skips the amount of input specified by the skip
argument, reads in another block of values, again skips input, and so on, until size
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.
If the input stream is bytes and fread
reaches the end of file 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.
Examples
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 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.
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.
fopen
for information about reading Big and Little Endian files.
No comments:
Post a Comment