Open file, or obtain information about open files
fileID = fopen( opens
filename, for binary read access, and
returns an integer file identifier equal to or greater than 3. MATLAB® reserves
standard input, standard output (the screen), and standard error,
fopen cannot open the file, then
[fileID,errmsg] = fopen(___) additionally
returns a system-dependent error message if
to open the file. Otherwise,
errmsg is an empty
character vector. You can use this syntax with any of the input arguments
of the previous syntaxes.
fIDs = fopen('all') returns a row vector
containing the file identifiers of all open files. The identifiers
reserved for standard input, output, and error are not included. The
number of elements in the vector is equal to the number of open files.
filename = fopen( returns
the file name that a previous call to
when it opened the file specified by
output filename is resolved to the full path. The
does not read information from the file to determine the output value.
[filename,permission,machinefmt,encodingOut] = fopen( additionally
returns the permission, machine format, and encoding that a previous
fopen used when it opened the specified
file. If the file was opened in binary mode,
is a standard encoding scheme name.
not read information from the file to determine these output values.
fileID returns empty character vectors
for all output arguments.
Open a file and pass the file identifier to
fgetl function to read data.
Open the file,
airfoil.m, and obtain
the file identifier.
fileID = fopen('airfoil.m');
fileID to the
to read one line from the file. Then, close the file.
tline = fgetl(fileID); fclose(fileID)
Create a prompt to request the name of a file to open.
fopen cannot open the file, display the relevant
fileID = -1; errmsg = ''; while fileID < 0 disp(errmsg); filename = input('Open file: ', 's'); [fileID,errmsg] = fopen(filename); end
Open a file to write to a file using the Shift-JIS character encoding.
fileID = fopen('japanese_out.txt','w','n','Shift_JIS');
'w' input specifies write access, the
specifies native byte ordering, and
the character encoding scheme.
Suppose you previously opened a file using
fileID = fopen('airfoil.m');
Get the file identifiers of all open files.
fIDs = fopen('all')
fIDs = 3
Get the file name and character encoding for the open
~ in place of output arguments you want
[filename,~,~,encoding] = fopen(3)
filename = matlabroot\toolbox\matlab\demos\airfoil.m encoding = windows-1252
The output shown here is representative. Your results might differ.
filename— Name of file to open
Name of the file to open, including the file extension, specified
as a character row vector or a string scalar. If the file is not in
the current folder,
filename must include a full
or a relative path.
On UNIX® systems, if
fopen function expands the path to the current
or specified user's home directory, respectively.
If you open a file with read access and the file is
not in the current folder, then
along the MATLAB search path.
If you open a file with write or append access and
the file is not in the current folder, then
a file in the current directory.
permission— File access type
File access type, specified as a character vector or a string scalar. You can open a file in binary mode or in text mode. On UNIX systems, both translation modes have the same effect. To open a file in binary mode, specify one of the following.
Open file for reading.
Open or create new file for writing. Discard existing contents, if any.
Open or create new file for writing. Append data to the end of the file.
Open file for reading and writing.
Open or create new file for reading and writing. Discard existing contents, if any.
Open or create new file for reading and writing. Append data to the end of the file.
Open file for appending without automatic flushing of the current output buffer.
Open file for writing without automatic flushing of the current output buffer.
To open files in text mode, attach the letter
permission argument, such as
On Windows® systems, in text mode:
Read operations that encounter a carriage return followed
by a newline character (
'\r\n') remove the carriage
return from the input.
Write operations insert a carriage return before any newline character in the output.
Open or create a new file in text mode if you want to write
to it in MATLAB and then open it in Microsoft® Notepad, or
any text editor that does not recognize
a newline sequence. When writing to the file, end each line with
For an example, see
Otherwise, open files in binary mode for better performance.
To read and write to the same file:
Open the file with a value for
includes a plus sign,
read and write operations. For example, do not call
fread, unless you call
machinefmt— Order for reading or writing bytes or bits
Order for reading or writing bytes or bits in the file, specified as one of the following character vectors or string scalars.
Your system byte ordering (default)
Big-endian ordering, 64-bit long data type
Little-endian ordering, 64-bit long data type
By default, all currently supported platforms use little-endian ordering for new files. Existing binary files can use either big-endian or little-endian ordering.
encodingIn— Character encoding
Character encoding to use for subsequent read and write operations,
fwrite, specified as a character vector or
a string scalar. The character vector or string scalar must contain
a standard character encoding scheme name such as the following.
If you do not specify an encoding scheme,
files for processing using the default encoding for your system. For
more information, see Opening Files with Different Character Encodings.
If you specify a value for encoding that is not in the list of supported values, MATLAB issues a warning. Specifying other encoding names sometimes (but not always) produces correct results.
fileID— File identifier of an open file
File identifier of an open file, specified as an integer.
In most cases, it is not necessary to open a file
in text mode. MATLAB import functions, all UNIX applications,
and Microsoft Word and WordPad recognize
a newline indicator.