Quantcast

Documentation Center

  • Trial Software
  • Product Updates

fopen

Open file, or obtain information about open files

Syntax

  • fileID = fopen(filename) example
  • fileID = fopen(filename,permission)
  • fileID = fopen(filename,permission,machinefmt,encodingIn) example
  • [fileID,errmsg] = fopen(___) example
  • filename = fopen(fileID)
  • [filename,permission,machinefmt,encodingOut] = fopen(fileID) example

Description

example

fileID = fopen(filename) opens the file, 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(filename,permission) opens the file with the type of access specified by permission.

example

fileID = fopen(filename,permission,machinefmt,encodingIn) additionally specifies the order for reading or writing bytes or bits in the file using the machinefmt argument. The optional encodingIn argument specifies the character encoding scheme associated with the file.

example

[fileID,errmsg] = fopen(___) additionally returns a system-dependent error message if fopen fails to open the file. Otherwise, errmsg is an empty string. You can use this syntax with any of the input arguments of the previous syntaxes.

example

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(fileID) returns the file name that a previous call to fopen used when it opened the file specified by fileID. The output filename is resolved to the full path. The fopen function does not read information from the file to determine the output value.

example

[filename,permission,machinefmt,encodingOut] = fopen(fileID) additionally returns the permission, machine format, and encoding that a previous call to 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 strings for all output arguments.

Examples

expand all

Open File and Pass Identifier to File I/O Function

Open a file and pass the file identifier to the fgetl function to read data.

Open the file, airfoil.m, and obtain the file identifier.

fileID = fopen('airfoil.m');

Pass the fileID to the fgetl function to read one line from the file. Then, close the file.

tline = fgetl(fileID);
fclose(fileID)

Request Name of File to Open

Create a prompt to request the name of a file to open. If fopen cannot open the file, display the relevant error message.

fileID = -1;
errmsg = '';
while fileID < 0 
   disp(errmsg);
   filename = input('Open file: ', 's');
   [fileID,errmsg] = fopen(filename);
end

Open File for Writing and Specify Access Type, Writing Order, Character Encoding

Open a file to write to a file using the Shift-JIS character encoding.

fileID = fopen('japanese_out.txt','w','n','Shift_JIS');

The 'w' input specifies write access, the 'n' input specifies native byte ordering, and 'Shift_JIS' specifies the character encoding scheme.

Get Information About Open Files

Suppose you previously opened a file using fopen.

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 file. Use ~ in place of output arguments you want to omit.

[filename,~,~,encoding] = fopen(3)
filename =

matlabroot\toolbox\matlab\demos\airfoil.m


encoding =

windows-1252

The output shown here is representative. Your results might differ.

Input Arguments

expand all

filename — Name of file to openstring

Name of the file to open, including the file extension, specified as a string. If the file is not in the current folder, filename must include a full or a relative 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 the file is not in the current folder, then fopen searches 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 fopen creates a file in the current directory.

Example: 'myFile.txt'

Data Types: char

permission — File access type'r' (default) | 'w' | 'a' | 'r+' | 'w+' | 'a+' | 'A' | 'W' | ...

File access type, specified as a string. 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 strings.

'r'

Open file for reading.

'w'

Open or create new file for writing. Discard existing contents, if any.

'a'

Open or create new file for writing. Append data to the end of the file.

'r+'

Open file for reading and writing.

'w+'

Open or create new file for reading and writing. Discard existing contents, if any.

'a+'

Open or create new file for reading and writing. Append data to the end of the file.

'A'

Append without automatic flushing of the current output buffer. (Used with tape drives.)

'W'

Write without automatic flushing of the current output buffer. (Used with tape drives.)

To open files in text mode, attach the letter 't' to the permission argument, such as 'rt' or 'wt+'.

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 '\n' as a newline sequence. When writing to the file, end each line with '\r\n'. For an example, see fprintf. Otherwise, open files in binary mode for better performance.

To read and write to the same file:

  • Open the file with a value for permission that includes a plus sign, '+'.

  • Call fseek or frewind between read and write operations. For example, do not call fread followed by fwrite, or fwrite followed by fread, unless you call fseek or frewind between them.

machinefmt — Order for reading or writing bytes or bits'n' (default) | 'b' | 'l' | 's' | 'a' | ...

Order for reading or writing bytes or bits in the file, specified as one of the following strings.

'n' or 'native'

Your system byte ordering (default)

'b' or 'ieee-be'

Big-endian ordering

'l' or 'ieee-le'

Little-endian ordering

's' or 'ieee-be.l64'

Big-endian ordering, 64-bit long data type

'a' or 'ieee-le.l64'

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'UTF-8' | 'ISO-8859-1' | 'windows-1251' | 'windows-1252' | ...

Character encoding to use for subsequent read and write operations, including fscanf, fprintf, fgetl, fgets, fread, and fwrite, specified as one of the following strings.

'Big5'

'ISO-8859-1'

'windows-932'

'EUC-JP'

'ISO-8859-2'

'windows-936'

'GBK'

'ISO-8859-3'

'windows-949'

'KSC_5601'

'ISO-8859-4'

'windows-950'

'Macintosh'

'ISO-8859-9'

'windows-1250'

'Shift_JIS'

'ISO-8859-13'

'windows-1251'

'US-ASCII'

'ISO-8859-15'

'windows-1252'

'UTF-8'

 

'windows-1253'

  

'windows-1254'

  

'windows-1257'

The default encoding is system-dependent.

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.

Data Types: char

fileID — File identifier of an open fileinteger

File identifier of an open file, specified as an integer.

Data Types: double

More About

expand all

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.

See Also

| | | | | | | | |

Was this topic helpful?