In this post we
will discuss some basic functions to work with external files using MATLAB.
Matlab can interact with many popular file fromats. The common file formats are
Excel® spread sheets, text, images, audio and video, XML documents and
scientific data formats. In addition to the functions to directly work with
these file format, there are low-level file I/O functions which work with data
files in any format.
Open( ) - Open the files with extension
The open
command is used to open a file available in the Matlab path. The file name is specified
with the extension. This function is used to open almost all common file types.
The syntax is:
open NAME
where NAME is
the filename with extension. If the extension is not specified, it will search
for a variable with name NAME and if not found only look for the files. The
path for the file also can be used in the command. If the file is not found in
the Matlab path of the specified path, the function will give an error message.
Examples:
Figure shows
the outputs of the different open commands.
open test.txt
open test.docx
open test.pptx
open test.xlsx
If the file
extension is not specified it will give an error message
open test
Error using
open (line 102)
File 'test'
not found.
fopen( ) - Open file
with access permission
fopen( ) is the
function used to open the files located in the current folder or Matlab path with
assigned permission modes. The syntax is;
FID = fopen(‘Filename’,
’Permission’)
The command opens
the file Filename in the mode specified by Permission. Default mode of permission
is ‘read access’. The file name is specified as a string with extension. The
file name may include the path of the file. FID is a scalar MATLAB integer
valued double, called a file identifier. The codes used for the permission are
given below.
'r'
open file for reading
'w'
open file for writing; discard existing contents
'a'
open or create file for writing; append data to end of file
'r+'
open (do not create) file for reading and writing
'w+'
open or create file for reading and writing; discard
existing contents
'a+'
open or create file for reading and writing; append data
to end of file
'W'
open file for writing without automatic flushing
'A'
open file for appending without automatic flushing
no = [1 2
3];
id =
[331560 331566 332563];
A= [no;
id];
fileID =
fopen('test.txt','w');
fprintf(fileID,'%6s %12s\n','No','ID');
fprintf(fileID,'%6.0f %12.0f\n',A);
fclose(fileID);
The script open
the file test.txt and write the data in to the file.
The fclose( ) function is
used to close the file.
If the file is
opened in update mode ('+'), we have to use fseek( ) or frewind( ) between other
commands for entering data(input) to or reading(output) data from the file.
[FID, MESSAGE]
= fopen('Filename') can be used to return a system dependent error message if
the open is not successful.
[FID, MESSAGE]
= fopen('data.txt')
FID =
-1
MESSAGE =
No such file or
directory
fclose( ) - Close the file
fclose(FID)
closes the file associated with file identifier FID. The FID must me the same
variable name used with the fopen( ) command.
If FID does not represent an open file, or if it is equal to 0 (standard
input), 1 (standard output), or 2 (standard error), fclose(FID) returns an
error.
>>
s=fopen(FID)
s =
C:\Users\skoya\Google Drive\Matlab Work\test.txt
s = fclose('all')
closes all open
files, except 0, 1 and 2.