MATLAB Function Reference
  Go to function:
    Search    Help Desk 
fread    See Also

Read binary data from file

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. Valid options are:

n
Reads n elements into a column vector.
inf
Reads to the end of the file, resulting in a column vector containing the same number of elements as are in the file.
[m,n]
Reads enough elements to fill an m-by-n matrix, filling in elements in column order, padding with zeros if the file is too small to fill the matrix.

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.

precision is a string representing the numeric precision of the values read, precision controls the number of bits read for each value and the interpretation of those bits as an integer, a floating-point value, or a character. The precision string may contain a positive integer repetition factor of the form 'n*' which prepends one of the strings above, like '40*uchar'. If precision is not specified, the default is 'uchar' (8-bit unsigned character) is assumed. See "Remarks" for more information.

[A,count] = fread(fid,size,precision,skip) includes an optional skip argument that specifies the number of bytes to skip after each precision value is read. With the skip argument present, fread reads in one value and does a skip of input, reads in another value and does a skip of input, etc. for at most size times. This is useful for extracting data in noncontiguous fields from fixed length records. If precision is a bit format like 'bitN' or 'ubitN', skip is specified in bits.

Remarks

Numeric precisions can differ depending on how numbers are represented in your computer's architecture, as well as by the type of compiler used to produce executable code for your computer.

The tables below give C-compliant, platform-independent numeric precision string formats that you should use whenever you want your code to be portable.

For convenience, MATLAB accepts some C and Fortran data type equivalents for the MATLAB precisions listed. If you are a C or Fortran programmer, you may find it more convenient to use the names of the data types in the language with which you are most familiar

MATLAB
C or Fortran
Interpretation
'char'
'char*1'
Character; 8 bits
'schar'
'signed char'
Signed character; 8 bits
'uchar'
'unsigned char'
Unsigned 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
'uint64'
'integer*8'
Unsigned integer; 64 bits
'float32'
'real*4'
Floating-point; 32 bits
'float64'
'real*8'
Floating-point; 64 bits
.

If you always work on the same platform and don't care about portability, these platform-dependent numeric precision string formats are also available:

MATLAB
C or Fortran
Interpretation
'short'
'short'
Integer; 16 bits
'int'
'int'
Integer; 32 bits
'long'
'long'
Integer; 32 or 64 bits
'ushort'
'usigned short'
Unsigned integer; 16 bits
'uint'
'unsigned int'
Unsigned integer; 32 bits
'ulong'
'unsigned long'
Unsigned integer; 32 or 64 bits
'float'
'float'
Floating-point; 32 bits
'double'
'double'
Floating-point; 64 bits

Two formats map to an input steam 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)

See Also

fclose      Close one or more open files

ferror      Query MATLAB about errors in file input or output

fopen       Open a file or obtain information about open files

fprintf     Write formatted data to file

fscanf      Read formatted data from file

fseek       Set file position indicator

ftell       Get file position indicator

fwrite      Write binary data from a MATLAB matrix to a file



[ Previous | Help Desk | Next ]