Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Revison: 1.0
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.
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.
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.
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
WK1READ
WK1WRITE
IMREAD
AUREAD
WAVREAD