Está en la página 1de 3

1403

How can I read my data file into


MATLAB?

Revison: 1.0

Last Date Modified: 31-August-2000

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, please see solution 9540. 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 MATfiles. In most cases, the syntax is quite simple to use these routines. If you would like to see an example
of how these routines are used, a file called highlevel.m can be downloaded from our FTP site at:
ftp://ftp.mathworks.com/pub/tech-support/solutions/tn1403/highlevel.m
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, please see solution 9982.

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, please see the help entry for TEXTREAD.
For example, if you had a file that contained the following:
Name
Sally
Joe
Bill

Type
Type1
Type2
Type1

Score
12.34
23.54
34.90

Y
45
60
12

Y/N
Yes
No
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, please see solution 26207.

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
FSCANF
FPRINTF
FGETL
FTELL
FSEEK
FCLOSE

- Open a file
- Read formatted data from file
- Write formatted data to file
- Read line from file, discard newline character
- Return the current file position
- Set file position indicator
- 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. Please 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.
To find a very basic example of writing a file with these routines and then reading it back in, please find a
file called asciilowlevel.m on our FTP site at:
ftp://ftp.mathworks.com/pub/tech-support/solutions/tn1403/asciilowlevel.m
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, please see one of the following links:
Numeric file with a header: Solution 9254.
Reading mixed numeric and character data: Solution 6005.
Reading a file with NaNs and Infs: Solution 3018.

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
FREAD
FWRITE
FTELL
FSEEK
FCLOSE

- Open a file
- Binary file read
- Binary file write
- Return the current file position
- Set file position indicator
- 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. To see an example of using the low level
routines for reading from and writing to a binary file, pleaselook at the file called binarylowlevel.m on our
FTP site at:
ftp://ftp.mathworks.com/pub/tech-support/solutions/tn1403/binarylowlevel.m

OTHER FILE I\O ROUTINES


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, please 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
WK1WRITE
IMREAD
AUREAD
WAVREAD

- Lotus 1,2,3 Worksheet


- Write spreadsheet (WK1) file
- Image data (.TIFF, .BMP, .JPEG, .PCX, .HDF, .XWD)
- Sun AU audio file
- PC WAV audio file

También podría gustarte