Functions of fread in MATLAB

Source: Internet
Author: User
Tags repetition

Help fread
Fread read binary data from file.
A = fread (FID) reads binary data from the specified file
And writes it into matrix A. FID is an integer File
Identifier obtained from fopen. MATLAB reads the entire
File and positions the file pointer at the end of the file
(See feof for details ).
 
A = fread (FID, size) reads the number of elements specified
By size. Valid entries for size are:
N read n elements into a column vector.
INF read to the end of the file.
[M, N] Read elements to fill an M-by-n matrix, in Column order.
N can be INF, but M can't.
 
A = fread (FID, size, precision) reads the file according
The data format specified by the string precision.
Precision input commonly contains a datatype specifier like
'Int' or 'float', followed by an integer giving the size in
Bits. The size argument is optional when using this syntax.
 
Any of the following strings, either the MATLAB version, or
Their C or Fortran equivalent, may be used. If not specified,
The default precision is 'aar '.
Matlab c or Fortran description
& Apos.
'Schar ''' signed Char 'signed character, 8 bits.
'Int8'' integer * 1' integer, 8 bits.
'Int16'' integer * 2' integer, 16 bits.
'Int32 ''integer * 4' integer, 32 bits.
'Int64''' integer * 8' integer, 64 bits.
'Uint8' 'integer * 1' unsigned integer, 8 bits.
'Uint16' 'integer * 2' unsigned integer, 16 bits.
'Uint32 ''integer * 4' unsigned integer, 32 bits.
'Int64''' integer * 8' unsigned integer, 64 bits.
'Single ''' real * 4' floating point, 32 bits.
'Float32' 'real * 4' floating point, 32 bits.
'Double' 'real * 8' floating point, 64 bits.
'Float64' 'real * 8' floating point, 64 bits.
 
The following platform dependent formats are also supported
They are not guaranteed to be the same size on all platforms.
 
Matlab c or Fortran description
'Char '''char * 1' character, 8 bits (signed or unsigned ).
'Short ''' integer, 16 bits.
'Int' integer, 32 bits.
'Long' integer, 32 or 64 bits.
'Ushort ''' unsigned short 'unsigned integer, 16 bits.
'Uint' 'unsigned int' unsigned integer, 32 bits.
'Ullong' 'unsigned long' unsigned integer, 32 bits or 64 bits.
'Float' floating point, 32 bits.
 
The following formats map to an input stream of bits rather
Bytes.
 
'Bitn' signed integer, N bits (1 <= n <= 64 ).
'Ubitn' unsigned integer, N bits (1 <= n <= 64 ).
 
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
Input stream is bits, then the partial result is returned as
Last value. If an error occurs before reaching the end of file,
Only full Elements read up to that point are used.
 
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 by '=>', and finally specifying your
Destination format. If the Source and Destination formats are
Same then the following shorthand notation may be used:
 
* Source
 
Which means:
 
Source => Source
 
For example,
 
Uint8 => uint8 read in unsigned 8-bit integers and
Save them in an unsigned 8-bit integer
Array
 
* Uint8 shorthand version of previous example
 
Bit4 => int8 read in signed 4-bit integers packed
In bytes and save them in a signed
8-bit integer array (each 4-bit
Integer becomes one 8-bit integer)
 
Double => real * 4 read in doubles, convert and save
As a 32-bit floating point Array
 
A = fread (FID, size, precision, skip) between des a skip argument that
Specifies the number of bytes to skip after each precision value
Is read. If precision specifies a bit source format, like 'bitn' or
'Ubitn', the Skip argument is interpreted as the number of bits
Skip. The size argument is optional when using this syntax.
 
When Skip is used, the precision string may contain a positive
Integer repetition factor of the form 'n' * 'Which prepends the source
Format of the precision argument, like '40 * Ar '. Note that 40 * uchar
For the precision alone is equivalent to '40 * uchar => double', not
'40 * uchar => uchar '. With Skip specified, fread reads in, at most,
Repetition factor number of values (default of 1), does a skip
Input specified by the Skip argument, reads in another block of Values
And does a skip of input, etc. Until size number of values have been
Read. If a skip argument is not specified, the repetition factor is
Ignored. repetition with Skip is useful for extracting data in
Noncontiguous fields from fixed length records.
 
For example,
 
S = fread (FID, 120, '40 * uchar => uchar ', 8 );
 
Reads in 120 characters in blocks of 40 each separated by 8
Characters.
 
A = fread (FID, size, precision, Skip, machineformat) treats the data
Read as having a format given by the string machineformat. You
Can obtain the machineformat argument from the output of
Fopen function. The size and skip arguments are optional when
Using this syntax.

Machineformat is one of the following strings:
 
Full precision support:
'Native 'or 'N'-Local Machine format-the default
'Ieee-le' or 'l'-IEEE floating point with little-Endian
Byte Ordering
'Ieee-be' or 'B'-IEEE floating point with big-Endian
Byte Ordering
'Vaxd' or 'D'-vax d floating point and VAX Ordering
'Vaxg' or 'G'-VAX g floating point and VAX Ordering
'Ieee-le. l64' or 'A'-IEEE floating point with little-Endian
Byte ordering and 64 bit long data type
'Ieee-be. l64' or 'S'-IEEE floating point with big-Endian byte
Ordering and 64 bit long data type.

Limited precision support: (double or equivalent)
'Cray' or 'C'-Cray floating point with big-Endian
Byte Ordering
 
See fopen on how to read big and little endian files.
 
[A, Count] = fread (...) Optional output argument count returns
The number of elements successfully read.
 
Examples:
 
The file alphabet.txt contains the 26 letters of the English
Alphabet, all capitalized. open the file for read access
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:
 
FID = fopen('alphabet.txt ', 'R ');
C = fread (FID, 5 )'
C =
65 66 67 68 69
Fclose (FID );
 
This time, specify that you want each element read as an unsigned
8-bit integer and output as a character. (using a precision
'Char => char 'or' * char 'will produce the same result ):
 
FID = fopen('alphabet.txt ', 'R ');
C = fread (FID, 5, 'uint8 => char ')'
C =
ABCDE
Fclose (FID );
 
See also fwrite, fseek, fscanf, fgetl, fgets, load, fopen, feof.

Overloaded functions or methods (ones with the same name in other directories)
Help serial/fread. m
Help UDP/fread. m
Help icinterface/fread. m

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.