MATLAB Function Reference
  Go to function:
    Search    Help Desk 
fopen    See Also

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.

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

1
Standard output, which is always open for appending (permission set to 'a')
2
Standard error, which is always open for appending (permission set to 'a')

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 can be:

'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 a 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

Files can be opened in binary mode (the default) or in text mode and for some systems, you must make the distinction when you use fopen. On PC and VMS systems, you must distinguish between text and binary mode. On UNIX systems, you do not need to distinguish between binary and text mode. In text mode, line separators are deleted on input before they reach MATLAB and are added for output. In binary mode, line separators are not deleted or added. To open a file in text mode, add a 't' to the permission string, for example, 'rt', which forces the file to be opened in text mode. 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.

format can be:

'cray' or 'c'

Cray floating point with big-endian byte ordering
'ieee-be' or 'b'

IEEE floating point with big-endian byte ordering
'ieee-le' or 'l'

IEEE floating point with little-endian byte ordering
'ieee-be.l64' or 's'

IEEE floating point with big-endian byte ordering and 64-bit long data type
'ieee-le.l64' or 'a'

IEEE floating point with little-endian byte ordering and 64-bit long data type
'native' or 'n'

the numeric format of the machine you are currently running
'vaxd' or 'd'

VAX D floating point and VAX ordering
'vaxg' or 'g'

VAX G floating point and VAX ordering

fids = fopen('all') returns a row vector containing the file identifiers of all open files, not including 1 and 2 (standard output and standard 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, ferror, fprintf, fread, fscanf, fseek, ftell, fwrite



[ Previous | Help Desk | Next ]