| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
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. Specify machineformat to:
Possible values are:
Windows systems use little-endian ordering, and most UNIX systems use big-endian ordering, for both bytes and bits. Solaris systems use big-endian ordering for bytes, but little-endian ordering for bits.
| ||||||||||||||||||||||||||||||
encoding |
String that specifies the character encoding scheme to use for subsequent read and write operations, including fscanf, fprintf, fgetl, fgets, fread, and fwrite. Possible values are:
Default: system-dependent |
fileID |
An integer that identifies the file for all subsequent low-level file I/O operations. If fopen cannot open the file, fileID is -1. MATLAB reserves file identifiers 0, 1, and 2 for standard input, standard output (the screen), and standard error, respectively. When fopen successfully opens a file, it returns a file identifier greater than or equal to 3. |
message |
A system-dependent error message when fopen cannot open the specified file. Otherwise, an empty string. |
fIDs |
Row vector containing the identifiers for all open files, except the identifiers reserved for standard input, output, and error. The number of elements in the vector is equal to the number of open files. |
filename |
Name of the file associated with the specified fileID. |
permission |
The permission that fopen assigned to the file specified by fileID. |
machineformat |
The value of machineformat that fopen used when it opened the file specified by fileID. |
encoding |
The character encoding scheme that fopen associated with the file specified by fileID. The value that fopen returns for encoding is a standard character encoding scheme name. It is not always the same as the encoding argument that you used in the call to fopen to open the file. |
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');
![]() | fminsearch | fopen (serial) | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |