Matlab Read file various methods

Source: Internet
Author: User
Tags numeric first row

Transfer from http://www.cnblogs.com/xianghang123/archive/2011/12/06/2277602.html

This technical support guide mainly deals with: ASCII, binary, and MAT files.
To get a complete list of functions that can be used in MATLAB to read and write various file formats, type the following command:
Help Iofun

There are two types of file I/O programs in MATLAB: High levels and low level.
High level routines: includes out-of-the-box functions that can be used to read and write data in special formats and require only a small amount of programming.
Low level routines: more flexible to perform relative special tasks, requiring more additional programming.

The high level routines includes out-of-the-box functions that can be used to read and write data in special formats and require only a small amount of programming.

For example, if you have a text file that contains numeric and alphabetic characters (text files) that you want to import into MATLAB, you can call some low-level routines to write a function yourself, or simply use the Textread function.

the key to using the high level routines is that the file must be similar (homogeneous), in other words, the file must have a consistent format. The following paragraphs describe some high-level file I/O routines and give some examples to help understand the concepts.

Load/save

The main high-level file I/O routines is the load and save function. LOAD
You can read mat-file data or ASCII data in a format similar to the space interval. Save can write MATLAB variables to ASCII data in mat-file format or space interval. In most cases, the syntax is fairly simple. The following example uses the ASCII file Sample_file.txt with numeric values separated by spaces:

1 5 4) 16 8

5 43 2) 6 8

6 8 4) 32 1

90 7 8) 7 6

5 9 81) 2 3

Example:
Read and write data with LOAD and SAVE


CODE:

% Load the file to the matrix, M:
M = Load (' sample_file.txt ')

% ADD 5 to M:
m = M +5

% Save M to a. mat file called ' Sample_file_plus5.mat ':
Save SAMPLE_FILE_PLUS5 M

% Save M to an ASCII. txt file called ' Sample_file_plus5.txt ':
Save Sample_file_plus5.txt M-ascii

Uigetfile/uiputfile

Uigetfile/uiputfile is based on the graphical user interface (GUI). A dialog box pops up to list the files and directories of the current directory, prompting you to select a file. Uigetfile lets you select a file to write (similar to the Windows ' Save as ' option. )。 With Uigetfile, you can select existing files to overwrite, or you can enter a new file name. The return value of two functions is the selected file name and path.

Example:
Select a m-file from the current directory with Uigetfile


CODE:

% This command lists all the m-files in the current directory and
% returns the name and path of the selected file

[Fname,pname] = uigetfile (' *.m ', ' Sample Dialog Box ')


Note: Uigetfile can only select one file at a time.

Uiimport/importdata

Uiimport is a powerful, easy-to-use GUI-based high-level routine for reading complex data files. The file must also be homogeneous.

ImportData forms the function of Uiimport and does not open the GUI. ImportData can be used in functions or scripts because the GUI-based file import mechanism in a function or script is not ideal. The following example uses the file ' Sample_file2.txt ' that contains several lines of file headers and text, numeric data:

This is a file header.

This is a file is a example.

col1 col2 col3 Col4

A 1 4 612.000

B 1 4 613.000

C 1 4 614.000

D 1 4 615.000

Example:using ImportData to read in a file with headers, text, and numeric data


CODE:

% this reads in the file ' Sample_file2.txt ' and creates a
% structure D that contains both data and text data.
% Note The importdata command specifies a white space
% as the delimiter of the file, but ImportData can usually
% detect this in its own

D = ImportData (' sample_file2.txt ', ')% of the original is incorrect.
D = ImportData (' sample_file2.txt ')

You can see the real values in structure d by accessing the data and text fields of structure d, such as input:

data = D.data

Text = D.textdata

You can use Uiimport to read the same file and get the same structure.

Note: For ASCII data, you must verify that the Import Wizard correctly recognizes the column delimiter.

Textread/strread

Textread is a powerful dynamic high-level routine designed to read ASCII-formatted text and/or numeric data files. Strread is similar to Textread except that it is read from a string rather than a file.

Two functions can use a number of parameters to change the way they work, and they return the data that is read into the specified output. They effectively provide you with a
The "Best of both worlds" approach because they can be read into mixed ASCII and numeric data with a single command (high-level routines approach), and you can change them to match your particular application (as low-level routines do). Example:


CODE:
Example 1:using Textread to read in an entire file into a cell array


% This command reads in the file fft.m into the cell array, file
File = Textread (' fft.m ', '%s ', ' delimiter ', ' \ n ', ' whitespace ', ');


CODE:
Example 2:using Strread to read the words in a line

% This command uses the cell, array created in Example 1 to
% read in each word of "line" in the ' file ' to a cell array, words

Words = Strread (file{28}, '%s ', ' delimiter ', ')


CODE:
Example 3:using Textread to read in text and numeric data from a file with headers

% This command skips the 2 header lines at the top of the file
% and reads in each column to the 4 specified outputs

[C1 c2 c3 C4] = textread (' sample_file2.txt ', '%s%s%s '%s ', ' Headerlines ', 2)

CODE:
Example 4:using Textread to read in specific rows of text and numeric data from a file

The% This command is reads in rows B and C of the file. The ' Headerlines '
Used to move down to the desired starting row and the
% read operation is performed 2 times

[C1 c2 c3 C4] = textread (' sample_file2.txt ',...
'%s%s%s ', 2, ' Headerlines ', 4)

CODE:
Example 5:using Textread to read in only the numeric data from a file containing text and numbers

The% this command reads the numeric data in the file. The
% ' Headerlines ' property was used to move down to the first row
% of interest and the first column of text is ignored with the
% ' * ' operator

[C2 c3 C4] = textread (' sample_file2.txt ', '%*s%d%d%f ', ' Headerlines ', 3)

Dlmread/dlmwrite/csvread

The Dlmread and Dlmwrite functions are capable of reading and writing delimited ASCII data, rather than using low-level routines. They are easier to use than low-level routines, and low-level routines can be reduced to one line with Dlmread/dlmwrite with a few lines of code.

Csvread is used to read a comma-delimited file, which is a special case of dlmread. Dlmread is especially useful when reading a space and tab-delimited spreadsheet file. Take ' sample_file.txt ' as an example:


CODE:
Example 1:using Dlmread to read in a file with headers, text, and numeric data

% this reads in the file ' Sample_file2.txt ' and creates a matrix, D,
% with the numeric data This command specifies a white space as the
% delimiter of the file

D = Dlmread (' sample_file.txt ', ')


CODE:
Example 2:using Dlmread to extract the first 3 columns of the last 3 rows

% this reads in the first 3 columns of the last 3 rows of
% The data file ' Sample_file.txt ' into the Matrix, d_partial.
% read file ' sample_file.txt ' first 3 columns after 3 lines, to matrix d_partial.

d_partial = Dlmread (' Sample_file.txt ', ', [2 0 4 2])



CODE:
Example 3:using Dlmwrite to write a comma delimited file

% this creates a file called ' PartialD.txt ' that consists of
% The first 3 columns of the last 3 rows of data where each
% element is separated by a comma

Dlmwrite (' PartialD.txt ', d_partial, ', ')


Note: The indicator dlmread and dlmwrite the specified range starts at 0 instead of 1.

Wk1read/wk1write

Wk1read is used to read Lotus123 data sheet files, Wk1write used to write matrices to Lotus123 spreadsheet files.

Xlsread

Xlsread is used to read the numeric and textual data of Excel.

three. Specific example analysis:
matlab Web site with two examples of the basic usage of each command, in fact, in the face of the data at hand, how to choose the appropriate command it. Here are a few examples to give some summary, you can extrapolate:

1. Pure data (the same number of columns):
source file:



CODE:
0 3866.162 2198.938 141.140
1 3741.139 2208.475 141.252
2 3866.200 2198.936 141.156
3 3678.048 2199.191 1 41.230
4 3685.453 2213.726 141.261
5 3728.769 2212.433 141.277
6 3738.785 2214.381 141.256
7 3728.759 2214 .261 141.228
8 3748.886 2214.299 141.243
9 3748.935 2212.417 141.253
3733.612 2226.653 141.236
11 3733. 583 2229.248 141.223
3729.229 2229.118 141.186




Answer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.