Matlab functions for reading/writing files

Rafael Palacios. Oct 2009
Tenured Assistant Professor. Instituto de Investigación Tecnológica, Universidad Pontificia Comillas
Visiting Assistan Profesor. Aeronautics and Astronautics Department. Massachusetts Institute of Technology


In this document we describe:



Native matlab format (load)

The two basic functions for loading and saving matlab variables are load and save
By default these functions read and write files in matlab format (.m extension) which are optimal in size, and compatible with matlab for all platforms (Windows, Linux, Solaris, MacOS)

load data load into the workspace all variables stored in data.m
save mydata saves all variables in the workspace into mydata.m
save mydata a b c saves variables a, b and c into file mydata.m


ASCII option of load

In adition to dealing with matlab native format, can also be used for ascii files.

save textfile.txt -ascii a
saves matrix a into textfile.txt, values separated by spaces
load textfile.txt loads your data creating a workspace variable called textfile
a=load('textfile.txt'); loads your data into variable a


Matlab importdata function

It is possible to import "any" kind of file by using importdata function, which in fact calls other input functions:
Unfortunately it doesn't use tdfread, which is the most interesting functions for reading text files if the file contains numbers and text.
Importdata can return unexpected variables. Reading text files it tends to returns single-column arrays of cells with one line of text per element.

CSV files (comma separated values)

Matlab can read and write CSV files if they only contain numeric values. CSV files can also exported/imported in Excel, however Excel is not restricted to numeric values

csvwrite('textfile.csv',mat);
mat=csvread('textfile.csv');


Delimitted text files (dlmread)

The most general Matlab function for reading text files is dlmread, which can be used to import data from a text file defining the delimiter that was used to separate numbers.

M = dlmread(filename, delimiter);



Tables in text files (tblread, statistics toolbox)

If your text file contains a header row with variable names, and a first column with case names, and the rest of the file is a matrix with just numbers, then you can use tblread in the following form:
[data,varnames,casenames] = tblread(filename);

After this functions call, data will contain the values, varnames will be a matrix of char with the names of the first row and casenames will be a matrix of char with the names of the firlst column.

tblwrite does the oposite, and creates a text file with the data, the format will be:
tblwrite(data,varnames,casenames,filename)

Both tblread and tblwrite allowds you to specify a field delimitter other than the default (space).

TAB delimited files (tdfread, statistics toolbox)

Matlab can read files with values separated by TABs (called Tab delimitted files) with the function tdfread.
This is the only function able to read numbers and text from a text file.
In this case the first row in the file must contain variable names separated by TABs

tdfread('textfile.tab'); Creates column vector for each column in the file.  The name of the variable is the one provided in the header of the file.
z=tdfread('textfile.tab'); Creates the structure z with as many fields as columns in the file.  Field names are taken from the header of the file

If one column in the file contains text, a matrix of char is crated. The width of matrix generated is adjusted to the longest text in the column.

If the file textfile.tab is the following:
Latitude » Longitude » City
42.3584 » -71.0598 » Boston
40.4167 » -3.7003 » Madrid
41.8955 » 12.4823 » Rome


This is the behaviour of matlab commands:
>> z=tdfread('textfile.tab');
>> whos
  Name           Size            Bytes  Class     Attributes
  City           3x6                36  char               
  Latitude       3x1                24  double             
  Longitude      3x1                24  double             
  z              1x1               456  struct             
>> z
z =
     Latitude: [3x1 double]
    Longitude: [3x1 double]
         City: [3x6 char]
>> tdfwrite('newfile.tab',z);

The file newfile.tab generated will look like this:
42.358400000000003 » -71.059799999999996 » Boston
40.416699999999999 » -3.7002999999999999 » Madrid
41.895499999999998 » 12.4823 » Rome

Matlab didn't implement tdfwrite but a version is available at Matlab Central as function tdfwrite



Excel Files

Matlab can read Excel files directly, using xlsread function.  The most common uses of xlsread are the following
num = xlsread(filename);
[num,txt] = xlsread(filename);
[num,txt,all] = xlsread(filename);  %This syntax requires MS Excel to be installed in the system
num is a matrix with just the numerical values found in the table.
txt is a cell array with the textual information found in the table.
all is a cell array with numbers and text

This function is very slow, but it also allows range definitions which makes it faster.
In order to write XLS files from Matlab, the funcion xlswrite ir provided.


Very Large files: Excel 2003 is limitted to 256 columns and 65,535 rows, and Excel 2007 is limitted to 16,384 columns and 1,048,576 rows. However, matlab can read larger files in other format such as CSV.