| MATLAB Function Reference | Search  Help Desk |
| fopen | See Also |
Open a file or obtain information about open files
Syntax
fid = fopen(filename,permission)
[fid,message] = fopen(filename,permission,format)
fids = fopen('all')
[filename,permission, format] = fopen(fid)
Description
Iffopen 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:'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: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