www.delorie.com/gnu/docs/octave/octave_121.html   search  
 
Buy GNU books!


GNU Octave

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

13.2.16 Binary I/O

Octave can read and write binary data using the functions fread and 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.

Built-in Function: [val, count] = fread (fid, size, precision, skip, arch)
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

Inf
Read as much as possible, returning a column vector.

nr
Read up to nr elements, returning a column vector.

[nr, Inf]
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.

[nr, nc]
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 Inf is assumed.

The optional argument precision is a string specifying the type of data to read and may be one of

"char"
"char*1"
"integer*1"
"int8"
Single character.

"signed char"
"schar"
Signed character.

"unsigned char"
"uchar"
Unsigned character.

"short"
Short integer.

"unsigned short"
"ushort"
Unsigned short integer.

"int"
Integer.

"unsigned int"
"uint"
Unsigned integer.

"long"
Long integer.

"unsigned long"
"ulong"
Unsigned long integer.

"float"
"float32"
"real*4"
Single precision float.

"double"
"float64"
"real*8"
Double precision float.

"integer*2"
"int16"
Two byte integer.

"integer*4"
"int32"
Four byte integer.

The default precision is "uchar".

The optional argument skip specifies the number of bytes to skip before each element is read. If it is not specified, a value of 0 is assumed.

The optional argument arch is a string specifying the data format for the file. Valid values are

"native"
The format of the current machine.

"ieee-le"
IEEE big endian.

"ieee-be"
IEEE little endian.

"vaxd"
VAX D floating format.

"vaxg"
VAX G floating format.

"cray"
Cray floating format.

Conversions are currently only supported for "ieee-be" and "ieee-le" formats.

The data read from the file is returned in val, and the number of values read is returned in count

Built-in Function: count = fwrite (fid, data, precision, skip, arch)
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 fread.

The behavior of fwrite is undefined if the values in data are too large to fit in the specified precision.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright © 2003   by The Free Software Foundation     Updated Jun 2003