MATLAB Programming/Reading and writing from files

Excel filesEdit

- xlsread, xlswrite

Delimited filesEdit

- textscan, dlmread, dlmwrite

Low-level functionsEdit

If you are familiar with reading and writing files in C, these functions will seem familiar. To other users, they are useful if more control is needed over how files are read (or if the built-in and MATLAB contributor central functions fail to read the file).

Opening and closing filesEdit

In order to use the low-level functions to read and write to a file, you have to open the file first. You also have to specify whether you want to read or write to the file, and whether or not you want a new file to be created. For example,

>> fid = fopen('TEXTFILE.txt', 'r'); % Read-only (creates a file if there is none there)
>> fid = fopen('TEXTFILE.txt', 'w'); % Write to an existing file (overwrite what is already there)
>> fid = fopen('TEXTFILE.txt', 'w+'); % Write to an existing file or create a new file (overwrite over existing contents)
>> fid = fopen('TEXTFILE.txt', 'a'); % Write to existing file but append to what is already there

There are other permissions as well, see the fopen function documentation for more details.

By default (if you don't specify a permission), you only have read permission when you open a file.

A little note about file ID's in MATLAB: a file identifier will actually be a number, and some numbers have special meanings:

fid = -1 ---> cannot open file
fid =  1 ---> standard output
fid =  2 ---> standard error

When you are done with a file, it is a good idea to close it so that other programs can use it, and so you dont accidentally overwrite data. Closing a file is done with the fclose function:

>> fclose(fid);

Format specifiersEdit

Low-level text reading functions depend on you, the user, to tell them what kind of data is to be read or written. However, they provide a great deal of control over the precision of inputs and outputs and how they are to be labeled. Format specifiers work by providing placeholders, and then providing data to splice into those placeholders. As an eaxmple, the following format specifier indicates that you want to read or write a string in the location specified by %s

>> spec = 'Bob has a daughter named %s';

%s is a placeholder for a string. You can also define placeholders for integers (%d), floating-point values (%x.yf, where x is the number of digits to print before the decimal point and y is the number to print afterward), %x for hexadecimal, and so on. Consult the fprintf documentation for details on the supported specifiers.

To read a file ("Daughters.txt" ) containing the following text:

Bob has a daughter named Jill

you would use the following commands:

>> fid = fopen('Daughters.txt');
>> spec = 'Bob has a daughter named %s'
>> s = fscanf(spec)
s = Jill
>> fclose(fid);

Note that only the string specified by %s is actually returned, but the entire string is read. If the file instead contained the following:

Bob has a son named Bob

and you ran the same code, you would obtain:

>> fid = fopen('Daughters.txt');
>> spec = 'Bob has a daughter named %s'
>> s = fscanf(spec);
s = 
>> fclose(fid);

Nothing is returned because there are no matches for 'Bob has a daughter named' in the beginning of the file. Note that nothing is returned from the following file as well:

Bob has a son named Bob
Bob has a daughter named Jill

We can only read "Bob has a daughter named Jill" after we have read "Bob has a son named Bob".

Vectorization of printing and scanning functionsEdit

Unlike the C version of fscanf and fprintf, the printing and scanning functions of MATLAB are "vectorized", which means you can pass a matrix to fprintf and have it print all of the lines for you with the same formatting, or you can ask fscanf to read each line of the file the same way and return a matrix to you. This only works reliably for numeric matrices. However, there are other, easier-to-use functions available that perform the same tasks with a performance penalty.

If you have an all-numeric matrix, the dlmread and dlmwrite functions work just as well unless a certain numeric precision is required for input or output.

To read cell arrays from a text file that contain strings, it is recommended to look on MATLAB contributor central for a routine called readtext, which is as easy to use as xlsread/xlswrite, and returns results in a comparable format. Reading such files can also be done using the textscan function, as long as the format of all of the lines is consistent (i.e. no columns mixing strings and numeric values, no blank cells, and so on).

Similarly, writing cell arrays to text can be performed using this file from MATLAB contributor central. Unfortunately, there is no built-in equivalent to textscan to write formatted data from a cell array to a text file.

Last modified on 20 March 2012, at 21:37