fopen
Open file or obtain information about open files
Syntax
Description
fileID = fopen(
opens
the file, filename
)filename
, for binary read access, and
returns an integer file identifier equal to or greater than 3. MATLAB® reserves
file identifiers 0
, 1
, and 2
for
standard input, standard output (the screen), and standard error,
respectively.
If fopen
cannot open the file, then fileID
is -1
.
fileID = fopen(
opens
the file with the type of access specified by filename
,permission
)permission
.
fileID = fopen(
additionally
specifies the order for reading or writing bytes or bits in the file
using the filename
,permission
,machinefmt
,encodingIn
)machinefmt
argument. The optional encodingIn
argument
specifies the character encoding scheme associated with the file.
[fileID,errmsg] = fopen(___)
additionally
returns a system-dependent error message if fopen
fails
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.
filename = fopen(
returns the filename that a
previous call to fileID
)fopen
used when it opened the file specified by
fileID
. The output filename
contains the
full path only if the previous fopen
call opened the file with
r
or r+
permissions. The
fopen
function 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
call to fileID
)fopen
used when it opened the specified
file. If the file was opened in binary mode, permission
includes
the letter 'b'
. The encodingOut
output
is a standard encoding scheme name. fopen
does
not read information from the file to determine these output values.
An invalid fileID
returns empty character vectors
for all output arguments.
Examples
Input Arguments
Limitations
MATLAB does not support internet URLs that require authentication.
MATLAB Online™ supports internet URLs associated with Microsoft OneDrive™ files and folders, while the installed version of MATLAB supports only local OneDrive files.
Tips
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
'\n'
as a newline indicator.