| Contents | Index |
fileID = fopen(filename)
fileID = fopen(filename, permission)
fileID = fopen(filename, permission, machineformat)
fileID = fopen(filename, permission, machineformat, encoding)
[fileID, message]
= fopen(filename, ...)
fIDs = fopen('all')
[filename, permission, machineformat, encoding]
= fopen(fileID)
fileID = fopen(filename) opens the file filename for read access, and returns an integer file identifier.
fileID = fopen(filename, permission) opens the file with the specified permission.
fileID = fopen(filename, permission, machineformat) specifies the order for reading or writing bytes or bits in the file.
fileID = fopen(filename, permission, machineformat, encoding) specifies the character encoding scheme associated with the file.
[fileID, message] = fopen(filename, ...) opens a file. If the operation fails, message is a system-dependent error message. Otherwise, message is an empty string.
fIDs = fopen('all') returns a row vector containing the file identifiers of all open files.
[filename, permission, machineformat, encoding] = fopen(fileID) returns the file name, permission, machine format, and encoding that a previous call to fopen used when it opened the specified file. fopen does not read information from the file to determine these output values. An invalid fileID returns empty strings for all output arguments.
filename |
String in single quotation marks that specifies the name of the file to open. Can include a full or partial path. On UNIX systems, if filename begins with '~/' or '~username/', the fopen function expands the path to the current or specified user's home directory, respectively. If you open a file with read access and fopen cannot find filename in the current folder, fopen searches along the MATLAB search path. Otherwise, fopen creates a file in the current directory. | ||||||||||||||||||||||||||||||
permission |
String that describes the type of access for the file: read, write, append, or update. Also specifies whether to open files in binary or text mode. To open files in binary mode, specify one of the following:
To read and write to the same file:
To open files in text mode, attach the letter 't' to the permission, such as 'rt' or 'wt+'. For better performance, do not use text mode. The following applies on Windows systems, in text mode:
This additional processing is unnecessary for most cases. All MATLAB import functions, and most text editors (including Microsoft Word and WordPad), recognize both '\r\n' and '\n' as newline sequences. However, when you create files for use in Microsoft Notepad, end each line with '\r\n'. For an example, see fprintf. | ||||||||||||||||||||||||||||||
machineformat |
String that specifies the order for reading or writing bytes or bits in the file. Possible values are:
By default, all currently supported platforms use little-endian ordering for new files. Existing binary files can use either big- or little-endian ordering. | ||||||||||||||||||||||||||||||
encoding |
String that specifies the character encoding scheme to use for subsequent read and write operations, including fscanf, fprintf, fgetl, fgets, fread, and fwrite. Supported values are:
For a list of additional encoding character sets, see http://www.iana.org/assignments/character-sets. If you specify a value for encoding that is not in the list of supported values, MATLAB issues a warning. Specifying other encodings sometimes (but not always) produces correct results. Default: system-dependent |
Open a file. Pass the file identifier, fid, to other file I/O functions to read data and close the file.
fid = fopen('fgetl.m');
tline = fgetl(fid);
while ischar(tline)
disp(tline);
tline = fgetl(fid);
end
fclose(fid);Create a prompt to request the name of a file to open. If fopen cannot open the file, display the relevant error message.
fid = -1;
msg = '';
while fid < 0
disp(msg);
filename = input('Open file: ', 's');
[fid,msg] = fopen(filename);
endOpen a file to write Unicode® characters to a file using the Shift-JIS character encoding scheme:
fid = fopen('japanese_out.txt', 'w', 'n', 'Shift_JIS');
fclose | feof | ferror | fprintf | fread | fscanf | fseek | ftell | fwrite

Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |