MATLAB - Cómo Leer Datos

4
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 MAT-files. 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

description

MATLAB - Cómo Leer Datos

Transcript of MATLAB - Cómo Leer Datos

Page 1: MATLAB - Cómo Leer Datos

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 MAT-files. 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 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

Page 2: MATLAB - Cómo Leer Datos

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 - Open a fileFSCANF - Read formatted data from fileFPRINTF - Write formatted data to fileFGETL - Read line from file, discard newline characterFTELL - Return the current file positionFSEEK - Set file position indicatorFCLOSE - 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 - Open a fileFREAD - Binary file readFWRITE - Binary file write

Page 3: MATLAB - Cómo Leer Datos

FTELL - Return the current file positionFSEEK - Set file position indicatorFCLOSE - 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 - Lotus 1,2,3 WorksheetWK1WRITE - Write spreadsheet (WK1) fileIMREAD - Image data (.TIFF, .BMP, .JPEG, .PCX, .HDF, .XWD)AUREAD - Sun AU audio fileWAVREAD - PC WAV audio file