MATLAB (4) : File Handling
MATLAB (4) : File Handling
Low-level file input and output is very similar to that in 'C', but with inherent vectorization. Files are opened with
>> fid = fopen(filename) >> fid = fopen(filename,mode) fid is a file identifier, used as a reference to the file for all subsequent read and write operations Files are closed with: >> fclose(fid)
fopen modes
'r' 'w' 'a' 'r+' 'w+' Open file for reading (default). Open file, or create new file, for writing; discard existing contents, if any. Open file, or create new file, for writing; append data to the end of the file. Open file for reading and writing. Open file, or create new file, for reading and writing; discard existing contents, if any. Open file, or create new file, for reading and writing; append data to the end of the file. Append without automatic flushing; used with tape drives. Write without automatic flushing; used with tape drives.
'a+'
'A' 'W'
>> fopen(filename,mode,format)
Opens a binary file and treats all data read or written as being of the specified format. This may be necessary to read binary files written under a different operating system.
FORMATS 'cray' or 'c' 'ieee-be' or 'b' 'ieee-le' or 'l' Cray floating point with big-endian byte ordering IEEE floating point with big-endian byte ordering IEEE floating point with little-endian byte ordering IEEE floating point with big-endian byte ordering and 64-bit long data type IEEE floating point with little-endian byte ordering and 64-bit long data type Numeric format of the machine on which MATLAB is running (the default) VAX D floating point and VAX ordering VAX G floating point and VAX ordering
'ieee-be.l64' or 's'
'ieee-le.l64' or 'a'
'native' or 'n'
'vaxd' or 'd' 'vaxg' or 'g'
valid precisions
MATLAB 'schar' 'uchar' 'int8' 'int16' 'int32' 'int64' 'uint8' 'uint16' 'uint32' 'uint64' 'float32' 'float64' 'double' C or Fortran 'signed char' 'unsigned char' 'integer*1' 'integer*2' 'integer*4' 'integer*8' 'integer*1' 'integer*2' 'integer*4' 'integer*8' 'real*4' 'real*8' 'real*8' Interpretation Signed character; 8 bits Unsigned character; 8 bits Integer; 8 bits Integer; 16 bits Integer; 32 bits Integer; 64 bits Unsigned integer; 8 bits Unsigned integer; 16 bits Unsigned integer; 32 bits Unsigned integer; 64 bits Floating-point; 32 bits Floating-point; 64 bits Floating-point; 64 bits
Format strings
The format string consists of ordinary characters, and/or conversion specifications indicating data type: %12e
Initial % character Conversion character
Field width
Add one or more of these characters between the % and the conversion character: An asterisk (*) Skip over the matched value. If %*d, then the value that matches d is ignored and is not stored. Maximum field width. For example, %10d. The size of the receiving object, for example, h for short, as in %hd for a short integer, or l for long, as in %ld for a long integer, or %lg for a double floatingpoint number.
A digit string
A letter
%c
%d %e, %f, %g %i
%o %s %u %x [...]
Sequence of characters; number specified by field width Base 10 integers Floating-point numbers Defaults to base 10 integers. Data starting with 0 is read as base 8. Data starting with 0x or 0X is read as base 16. Signed octal integer A series of non-white-space characters Signed decimal integer Signed hexadecimal integer Sequence of characters (scanlist)