Read the First Line Using Fread Matlab

fread

Read binary data from file

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 statement 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 cease of the file and the file pointer is at the stop of the file (come across feof for details). Valid options are:

north
Reads n elements into a cavalcade vector.
inf
Reads to the end of the file, resulting in a cavalcade vector containing the same number of elements equally are in the file.
[one thousand,north]
Reads enough elements to fill an m-by-due north matrix, filling in elements in column order, padding with zeros if the file is also pocket-sized to fill the matrix. due north tin can exist specified every bit inf, simply m cannot.

precision is a string that specifies the format of the data to be read. It commonly contains a datatype specifier such as int or bladder, followed by an integer giving the size in bits. Any of the strings in the following tabular array, either the MATLAB version or their C or Fortran equivalent, may be used. If precision is non specified, the default is 'uchar'.

MATLAB
C or Fortran
Estimation
'schar'
'signed char'
Signed grapheme; 8 bits
'uchar'
'unsigned char'
Unsigned character; 8 bits
'int8'
'integer*i'
Integer; 8 bits
'int16'
'integer*two'
Integer; 16 $.25
'int32'
'integer*4'
Integer; 32 bits
'int64'
'integer*8'
Integer; 64 bits
'uint8'
'integer*1'
Unsigned integer; viii bits
'uint16'
'integer*2'
Unsigned integer; 16 bits
'uint32'
'integer*four'
Unsigned integer; 32 bits
'uint64'
'integer*8'
Unsigned integer; 64 $.25
'float32'
'real*4'
Floating-bespeak; 32 bits
'float64'
'real*8'
Floating-point; 64 bits
'double'
'real*eight'
Floating-point; 64 bits

.

The following platform dependent formats are besides supported merely they are not guaranteed to be the same size on all platforms.

MATLAB
C or Fortran
Estimation
'char'
'char*one'
Character; 8 bits
'short'
'short'
Integer; sixteen bits
'int'
'int'
Integer; 32 bits
'long'
'long'
Integer; 32 or 64 bits
'ushort'
'unsigned short'
Unsigned integer; 16 bits
'uint'
'unsigned int'
Unsigned integer; 32 bits
'ulong'
'unsigned long'
Unsigned integer; 32 or 64 bits
'bladder'
'bladder'
Floating-point; 32 bits

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

By default, numeric values are returned in class double arrays. To render numeric values stored in classes other than double, create your precision argument by get-go specifying your source format, so following it with the characters "=>", and finally specifying your destination format. You lot are not required to use the exact name of a MATLAB class type for destination. (Run across class for details). fread translates the name to the most advisable MATLAB class blazon. If the source and destination formats are the aforementioned, the post-obit shorthand notation can exist used.

  • *source          

which ways

  • source=>source          

This table shows some example precision format strings.

'uint8=>uint8'
Read in unsigned viii-bit integers and save them in an unsigned 8-flake integer array.
'*uint8'
Shorthand version of the above.
'bit4=>int8'
Read in signed 4-bit integers packed in bytes and save them in a signed 8-chip array. Each 4-bit integer becomes an viii-scrap integer.
'double=>real*four'
Read in doubles, convert and save as a 32-bit floating point assortment.

[A,count] = fread(fid,size,precision , skip) includes an optional skip statement that specifies the number of bytes to skip afterward each precision value is read. If precision specifies a bit format, similar 'bitN' or 'ubitN', the skip argument is interpreted as the number of bits to skip.

When skip is used, the precision string may incorporate a positive integer repetition factor of the course 'Due north*' which prepends the source format specification, such as 'twoscore*uchar'.

    Annotation    Do non confuse the asterisk (*) used in the repetition factor with the asterisk used as precision format shorthand. The format string '40*uchar' is equivalent to 'xl*uchar=>double', not 'twoscore*uchar=>uchar'.

When skip is specified, fread reads in, at nigh, a repetition factor number of values (default is 1), skips the amount of input specified by the skip argument, reads in another cake of values, again skips input, and and then on, until size number of values accept been read. If a skip argument is not specified, the repetition factor is ignored. Use the repetition gene with the skip statement to excerpt data in noncontiguous fields from fixed length records.

If the input stream is bytes and fread reaches the end of file (run across feof) in the middle of reading the number of bytes required for an element, the fractional result is ignored. However, if the input stream is bits, then the partial outcome is returned equally the terminal value. If an fault occurs before reaching the end of file, merely full elements read up to that bespeak are used.

Examples

For example,

  • 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.thousand','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 unabridged file, converting the unsigned characters into a column vector of grade 'double' (double precision floating point). To display the event equally readable text, the 'double' column vector is transposed to a row vector and converted to class 'char' using the char function.

As another example,

  • s = fread(fid,120,'40*uchar=>uchar',8);          

reads in 120 characters in blocks of 40, each separated by viii characters. Annotation that the form type of southward is 'uint8' since information technology is the appropriate form corresponding to the destination format, 'uchar'. Also, since 40 evenly divides 120, the terminal cake read is a full block which means that a terminal skip will be washed before the command is finished. If the terminal block read is not a full block then fread will not finish with a skip.

Come across fopen for information about reading Big and Little Endian files.

See Also

fclose, ferror, fopen, fprintf, fread, fscanf, fseek, ftell, fwrite, feof

 frameedit fread (serial)

vinsonafror1997.blogspot.com

Source: http://www.ece.northwestern.edu/local-apps/matlabhelp/techdoc/ref/fread.html

0 Response to "Read the First Line Using Fread Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel