Skip to Main Content Skip to Search
Home |   United Kingdom  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Industries Academia Support User Community Company

 

Product Support

1403 - Reading Data Files into MATLAB


There are a number of file formats that can be read into MATLAB. To see a list of file formats that can be easily read in, see solution 1-16ESY. All of the file I/O routines are in MATLAB, so they require no special toolboxes. Below are examples of reading in formatted ASCII, MAT and binary files.

There are two basic types of file I/O routines in MATLAB, high level and low level. High level routines include ready made functions which read and write data in specific formats, while low level routines are much more flexible.

LOAD and SAVE

Our major high level file I/O routines are the LOAD and SAVE functions. These functions make it easy to save MATLAB variables into ASCII or MAT-files as well as load in homogeneous ASCII files and MAT-files. In most cases, the syntax is quite simple to use these routines. highlevel.m offers an example how these routines are used.

If you have MATLAB 5 or higher, you can use LOAD and SAVE with variable filenames without using the EVAL function. For examples of using the functional form of LOAD and SAVE, see solution 1-16FP4.

DLMREAD/DLMWRITE and TEXTREAD

The DLMREAD and TEXTREAD functions allow you to read in formatted ASCII data without using our low level routines. These functions are much easier to use than the low level routines, and what would take a number of lines of code with low level routines can be simplified to one line with DLMREAD and TEXTREAD.

If your data is all numeric, but separated by some delimiter (or you want to write such a file); DLMREAD allows you to read the data into one matrix, while ignoring your delimiter, and DLMWRITE allows you to write a file with such a format.

DLMREAD becomes especially helpful when trying to read in Excel files which can be saved as space or tab delimited. For example, if you save your Excel file as mydata.txt as tab delimited, the following statement reads in your data to a matrix, called M:

M = dlmread('mydata.txt','\t')  
	% \t indicates a tab
      

The TEXTREAD function allows you to read in mixed ASCII and numeric data with one command. This function also allows you to ignore headerlines, indicate a delimiter, and many other properties of a formatted file. To see a complete list of options, see the help entry for TEXTREAD.

For example, if you had a file that contained the following:

        Name    Type  Score Y  Y/N	
        Sally   Type1 12.34 45 Yes
        Joe     Type2 23.54 60 No
        Bill    Type1 34.90 12 No
      

and you want to read each column into a separate variable, you would use the following command in MATLAB:

        [names,types,score,y,answer] = 
	 textread('mydata.dat','%s %s %f %d %s','headerlines',1);
      

To see an example of reading in this data and then putting the data into variables which are the same as the column headers, see solution 1-17YYD.

LOW LEVEL ROUTINES FOR ASCII FILES

The low level file I/O routines in MATLAB are based on the same routines from C. The following is a list of commands used to read in ASCII files:

FOPEN - Open a file
FSCANF - Read formatted data from file
FPRINTF - Write formatted data to file
FGETL - Read line from file, discard newline character
FTELL - Return the current file position
FSEEK - Set file position indicator
FCLOSE - Close a file

Of course, the first step in reading in a file is to open the file. When a file is opened with FOPEN, a pointer is placed at the upper left hand corner of the file. This is where MATLAB will start reading. As other commands such as FSCANF or FPRINTF are used, the pointer inside the file moves as specified by the commands until the file is closed. Note, it is necessary to close the file after opening it because if the file isn't closed and you try to access it, a sharing violation will occur.

asciilowlevel.m offers a very basic example of writing a file with these routines and then reading it back in.

It becomes more difficult to read in files when you add things such as headers, mixed numeric and character data, or NANs and Infs. To see examples of using low level file I/O routines for these purposes, see one of the following links:

Numeric file with a header: Solution 1-16DQQ.

Reading mixed numeric and character data: Solution 1-15VVF.

Reading a file with NaNs and Infs: Solution 1-15M3Y.

LOW LEVEL ROUTINES FOR BINARY FILES

Like the low level routines for ASCII files, the low level file I/O routines for binary files in MATLAB are based on the same routines from C. The following is a list of commands used to read in binary files:

FOPEN - Open a file
FREAD - Binary file read
FWRITE - Binary file write
FTELL - Return the current file position
FSEEK - Set file position indicator
FCLOSE - Close a file

A binary file is opened and read or written in the same way ASCII files are; but since it is a binary file, you must know specific information about your file. For example, you need to know the size of the data you want to read in, and how many bytes each value uses. binarylowlevel.m offers an example of using the low level routines for reading from and writing to a binary file.

OTHER FILE I/O ROUTINES


Import Data to MATLAB from Excel

MATLAB also offers a number of other file I/O routines for reading and writing specific type of files. For information on syntax to use these functions, type 'help' without the quotes followed by the function name at the MATLAB command prompt.

Here is a list of other file I/O routines:

WK1READ - Lotus 1,2,3 Worksheet
WK1WRITE - Write spreadsheet (WK1) file
IMREAD - Image data (.TIFF, .BMP, .JPEG, .PCX, .HDF, .XWD)
AUREAD - Sun AU audio file
WAVREAD - PC WAV audio file

For information on importing data into MATLAB specifically from Excel, see Solution 1-19BU0.

Contact support
E-mail this page
Print this page