MATLAB Functions Help Desk

fopen

Purpose

Open a file or obtain information about open files

Syntax

Description

If fopen successfully opens a file, it returns a file identifier fid, and the value of message is empty. The file identifier can be used as the first argument to other file input/output routines. If fopen does not successfully open the file, it returns a -1 value for fid. In that case, the value of message is a string that helps you determine the type of error that occurred.

Three fids are predefined and cannot be explicitly opened or closed:

fid = fopen(filename,permission) opens the file filename in the mode specified by permission and returns fid, the file identifier. filename may a MATLABPATH relative partial pathname. If the file is opened for reading and it is not found in the current working directory, fopen searches down MATLAB's search path.

permission is one of the strings:

'r'

Open the file for reading (default).

'r+'

Open the file for reading and writing.

'w'

Delete the contents of an existing file or create a new file, and open it for writing.

'w+'

Delete the contents of an existing file or create new file, and open it for reading and writing.

'W'

Write without automatic flushing; used with tape drives

'a'

Create and open a new file or open an existing file for writing, appending to the end of the file.

'a+'

Create and open new file or open an existing file for reading and writing, appending to the end of the file.

'A'

Append without automatic flushing; used with tape drives

Add a 't' to these strings, for example, 'rt', on systems that distinguish between text and binary files, to force the file to be opened in text mode. Under DOS and VMS, for example, you cannot read a text file unless you set the permission to 'rt'. Similarly, use a 'b' to force the file to be opened in binary mode (the default).

[fid,message] = fopen(filename,permission,format) opens a file as above, returning file identifier and message. In addition, you specify the numeric format with format, a string defining the numeric format of the file, allowing you to share files between machines of different formats. If you omit the format argument, the numeric format of the local machine is used. Individual calls to fread or fwrite can override the numeric format specified in a call to fopen. Permitted format strings are:

'native' or 'n'

The numeric format of the machine you are currently running

'ieee-le' or 'l'

IEEE floating point with little-endian byte ordering

'ieee-be' or 'b'

IEEE floating point with big-endian byte ordering

'vaxd' or 'd'

VAX D floating point and VAX ordering

'vaxg' or 'g'

VAX G floating point and VAX ordering

'cray' or 'c'

Cray floating point with big-endian byte ordering

'ieee-le.l64' or 'a'

IEEE floating point with little-endian byte ordering and 64-bit long data type

'ieee-be.l64' or 's'

IEEE floating point with big-endian byte ordering and 64-bit long data type

fids = fopen('all') returns a row vector containing the file identifiers of all open files, not including 0, 1, and 2 (standard input, output, and error). The number of elements in the vector is equal to the number of open files.

[filename,permission,format] = fopen(fid) returns the full filename string, the permission string, and the format string associated with the specified file. An invalid fid returns empty strings for all output arguments. Both permission and format are optional.

See Also

fclose      Close one or more open files

ferror      Query MATLAB about errors in file input or output

fprintf     Write formatted data to file

fread       Read binary data from 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

See also partialpath.



[ Previous | Help Desk | Next ]