## FREAD File Read Function

Section: Input/Ouput Functions

### Usage

Reads a block of binary data from the given file handle into a variable of a given shape and precision. The general use of the function is

      A = fread(handle,size,precision)

The `handle` argument must be a valid value returned by the fopen function, and accessable for reading. The `size` argument determines the number of values read from the file. The `size` argument is simply a vector indicating the size of the array `A`. The `size` argument can also contain a single `inf` dimension, indicating that FreeMat should calculate the size of the array along that dimension so as to read as much data as possible from the file (see the examples listed below for more details). The data is stored as columns in the file, not rows. Alternately, you can specify two return values to the `fread` function, in which case the second value contains the number of elements read

       [A,count] = fread(...)

where `count` is the number of elements in `A`. The third argument determines the type of the data. Legal values for this argument are listed below:

- 'uint8','uchar','unsigned char' for an unsigned, 8-bit integer.
- 'int8','char','integer*1' for a signed, 8-bit integer.
- 'uint16','unsigned short' for an unsigned, 16-bit integer.
- 'int16','short','integer*2' for a signed, 16-bit integer.
- 'uint32','unsigned int' for an unsigned, 32-bit integer.
- 'int32','int','integer*4' for a signed, 32-bit integer.
- 'single','float32','float','real*4' for a 32-bit floating point.
- 'double','float64','real*8' for a 64-bit floating point.
- 'complex','complex*8' for a 64-bit complex floating point (32 bits for the real and imaginary part).
- 'dcomplex','complex*16' for a 128-bit complex floating point (64 bits for the real and imaginary part).

### Example

First, we create an array of `512 x 512` Gaussian-distributed `float` random variables, and then writing them to a file called `test.dat`.

    --> A = float(randn(512));
    --> fp = fopen('test.dat','wb');
    --> fwrite(fp,A);
    --> fclose(fp);
    -->
    quit

Read as many floats as possible into a row vector

    --> fp = fopen('test.dat','rb');
    --> x = fread(fp,[1,inf],'float');
    --> who x
      Variable Name      Type   Flags             Size
                  x     float                    [1 262144]
    -->
    quit

Read the same floats into a 2-D float array.

    --> fp = fopen('test.dat','rb');
    --> x = fread(fp,[512,inf],'float');
    --> who x
      Variable Name      Type   Flags             Size
                  x     float                    [512 512]
    -->
    quit