Octave can read and write binary data using the functions
fwrite, which are patterned after the standard C functions
with the same names. The are able to automatically swap the byte order
of integer data and convert among ths supported floating point formats
as the data are read.
Read binary data of type precision from the specified file ID fid.
The optional argument size specifies the amount of data to read and may be one of
- Read as much as possible, returning a column vector.
- Read up to nr elements, returning a column vector.
- Read as much as possible, returning a matrix with nr rows. If the number of elements read is not an exact multiple of nr, the last column is padded with zeros.
- Read up to nr
*nc elements, returning a matrix with nr rows. If the number of elements read is not an exact multiple of nr, the last column is padded with zeros.
If size is omitted, a value of
The optional argument precision is a string specifying the type of data to read and may be one of
- Signed character.
- Unsigned character.
- 8-bit signed integer.
- 16-bit signed integer.
- 32-bit signed integer.
- 64-bit signed integer.
- 8-bit unsigned integer.
- 16-bit unsigned integer.
- 32-bit unsigned integer.
- 64-bit unsigned integer.
- 32-bit floating point number.
- 64-bit floating point number.
- Single character.
- Short integer (size is platform dependent).
- Integer (size is platform dependent).
- Long integer (size is platform dependent).
- Unsigned short integer (size is platform dependent).
- Unsigned integer (size is platform dependent).
- Unsigned long integer (size is platform dependent).
- Single precision floating point number (size is platform dependent).
The default precision is
The precision argument may also specify an optional repeat count. For example, `32*single' causes
freadto read a block of 32 single precision floating point numbers. Reading in blocks is useful in combination with the skip argument.
The precision argument may also specify a type conversion. For example, `int16=>int32' causes
freadto read 16-bit integer values and return an array of 32-bit integer values. By default,
freadreturns a double precision array. The special form `*TYPE' is shorthand for `TYPE=>TYPE'.
The conversion and repeat counts may be combined. For example, `32*single=>single' causes
freadto read blocks of single precision floating point values and return an array of single precision values instead of the default array of double precision values.
The optional argument skip specifies the number of bytes to skip after each element (or block of elements) is read. If it is not specified, a value of 0 is assumed. If the final block read is not complete, the final skip is omitted. For example,fread (f, 10, "3*single=>single", 8)
will omit the final 8-byte skip because the last read will not be a complete block of 3 values.
The optional argument arch is a string specifying the data format for the file. Valid values are
- The format of the current machine.
- IEEE big endian.
- IEEE little endian.
- VAX D floating format.
- VAX G floating format.
- Cray floating format.
Conversions are currently only supported for
The data read from the file is returned in val, and the number of values read is returned in
Write data in binary form of type precision to the specified file ID fid, returning the number of values successfully written to the file.
The argument data is a matrix of values that are to be written to the file. The values are extracted in column-major order.
The remaining arguments precision, skip, and arch are optional, and are interpreted as described for
The behavior of
fwriteis undefined if the values in data are too large to fit in the specified precision.