III.
Analysis of specific examples:
The MATLAB website uses two examples to describe the basic usage of each command in great detail. In reality, how can we choose the appropriate command for the data on the opponent's head? The following is a summary based on several examples:
1. Pure data (with 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 141.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
10 3733.612 2226.653 141.236
11 3733.583 2229.248 141.223
12
3729.229 2229.118 141.186
A: For this TXT file, since the number of columns in each row is the same, load and importdata can be used simply.
2. Field name (both Chinese and English fields) + data:
Source file:
Code:
Ch0 CH2 CH3
0.000123 0.000325 0.000378
0.000598
0.000986 0.000256 0.000245 0.000698
A: because it is a record, the number of columns in each row must be the same (if some columns are missing, add inf or Nan in the file). Therefore, you can directly use importdata.
3. Comment (containing independent numeric strings) + data (with the same number of columns ):
Problem: this file has four columns, but the first six rows are text descriptions, and the four columns of numbers start from row 8th. now I want to propose the first two columns and text descriptions of this file to form a new DAT file.
Source file:
Code:
Group 2 12.02.2006 limei
Samples of data: 50000
Ch0 CH2 CH3
0.000123 0.000325 0.000378 0.000598
0.000986
0.000256 0.000245 0.000698
Target file:
Code:
Group 2 12.02.2006 limei
Samples of data: 50000
Ch0 success
0.000123 0.000325
0.000986 0.000256
A: Because the comment contains an independent numeric string and the comment part does not have an obvious format, it will fail to be directly read using advanced commands such as importdata and load.
Formatting commands such as textread and dlmwrite are not suitable, so they can only be read using low-level commands. (Of course, you can skip the comment and directly use advanced commands to read data, that is, [A B C
D] = textread (filename, '% F % F', 'headerlines', 4 );
). A simple and non-generic method for reading comments is as follows:
------------------------------------- Convert
Bytes ---------------------------------------------------------------------------------------
Code:
CLC; clear;
FID = fopen('exp.txt ',
'R ');
Fid_n = fopen ('ex. dat ', 'w ');
While ~ Feof (FID)
Tline = fgetl (FID );
If ~ Isempty (tline)
If
Double (tline (1) >=48 & Double (tline (1) <= 57% value start
A = strread (tline );
A (3: 4) = [];
Fprintf (fid_n, '% F
% F/N', );
Clear;
Elseif double (tline (1) = 67
% Letter C start
[B1, B2, B3, B4] = strread (tline, '% S % s ');
B = [B1 {1}, '', B2 {1}];
Fprintf (fid_n, '% S/N', B );
Clear B B1 B2 B3 B4;
Else
Fprintf (fid_n, '% S/N', tline );
End
Else
Fprintf (fid_n, '% S/N', tline );
End
End
Fclose (FID );
Fclose (fid_n );
---------------------------------------------------------------------------------
4. Comment (excluding independent numeric strings) + data (with the same number of columns ):
Source file:
Code:
Hello ABC
Welcome to us
Vibration Forum
Vib.hit.edu.cn
1 11 111 1111
2 22
222 2222
3 33 333 3333
4 44 444 4444
5 55 555 5555
A: You can directly use importdata.
Note: Sometimes an independent numeric string in the comment can be successfully importdata, but the result may be incorrect. In this case, we recommend that you use the reading method in 3rd cases.
5. Comment and data mixing:
Of course, you can only program this by yourself. For example:
Source file:
Code:
1 11 111 1111
Hi!
2 22 222 2222
Welcome
3 33 333
3333
Vibration Forum
4 44 444 4444
Vib.hit.edu.cn
5 55 555 5555
Answer:
-------------------------------------------- Convert --------------------------------------
Code:
Function
[Data] = distilldata (infile)
% Function description:
% Read the value data in the original file that saves the data into a data variable
% Instructions:
%
Infile -- original data file name;
% DATA = data variable
Tmpfile = 'tmp2. Mat ';
Fidin = fopen (infile, 'R'); % open the original data file (. List)
Fidtmp = fopen (tmpfile, 'w'); % create and save the data file (excluding instructions)
While ~ Feof (fidin) % determines whether it is the end of the file
Tline = fgetl (fidin); %
Read a line of text from a file (excluding the Enter key)
If ~ Isempty (tline) % determines whether the row is empty
[M, N] = size (tline );
Flag = 1;
For I = 1: N
% Determines whether a row contains any characters (except for the +-. ee and Space key)
If ~ (Tline (I) ='
'| Tline (I) ='-'| tline (I) ='. '| tline (I) = 'E '...
| Tline (I) = 'E' | tline (I) = '+ '...
| (Double (tline (I)> = 48 & Double (tline (I) <= 57 ))
Flag = 0;
Break;
End
End
If flags = 1%
If it is a numeric row, write this row of data to a file
Fprintf (fidtmp, '% S/N', tline );
End
End
End
Fclose (fidin );
Fclose (fidtmp );
Data = textread (tmpfile );
Delete (tmpfile );
Bytes ---------------------------------------------------------------------------------------------------------
In addition, you can also use
The textread function skips the annotation part for reading, but the premise is that you need to know the structure of the file content in advance (that is, which line is data and which line is comment)
6. Data separation of each column:
Source file:
Code:
0 + 47038.7 1.05 09:26:07 C
2 +
46477.7 1.03 09:28:38 C
4 + 44865.7 1.04 09:28:48 C
6 + 41786.4 1.03 09:28:56 C
8 + 39896.0
0.97 09:29:03 C
10 + 37518.4 0.93 09:29:15 C
12 + 35858.5 0.92 09:29:30 C
14 + 46105.0
1.03 09:30:21 C
16 + 46168.6 6.89 09:30:30 C
18 + 48672.3 4.33 09:30:40 C
20 + 49565.7
0.49 09:30:48 C
22 + 49580.7 0.53 09:30:55 C
24 + 49602.3 0.84 09:31:03 C
26 + 49582.5
1.51 09:31:11 C
28 + 49577.0 1.39 09:31:19 C
30 + 49589.3 0.61 09:31:27 C
32 + 49578.3
1.06 09:31:29 C
34 + 49512.5 1.77 09:31:38 C
A: directly use [a, B, c, d, e, f] = textread (yourfilename, '% d % C % F % S % C');
Iv. Notes:
1. In Matlab, keep the current path under the directory corresponding to the data file for access. Otherwise, provide the specific path of the data file during access.
2. Provide the full name of the data file (including the suffix, which can be omitted when reading the mat file) during access)
3. For the differences between load data.txtand aw.load('data.txt '), refer to the excellent post: [original] For beginners who want to learn MATLAB
4. open the file according to the read/write needs, that is, specify the permission attribute of fopen as read or write as needed. If only a is used for writing
Fread read. In this case, close the file, use R to open the file, or directly use A + for simultaneous read/write operations. Otherwise, an inexplicable problem may occur! The following code is an incorrect example:
Code:
Filename = 'E. dat ';
FID = fopen (filename, 'A ');
If FID <0
Error ('fopen error ');
End
S = [1 2 3 4; 5 6 7
8];
Fwrite (FID, S, 'float32 ')
[Dd
Ll] = fread (FID, INF, 'float32'); % read all the data in T, that is, the s matrix.
Fclose (FID );
The resulting DD, ll is incorrect and meaningless!
V. Other related questions:
1. read data from multiple files consecutively and store the data in a matrix:
(1)
First, how to read the file name:
Method 1:
Filename = Dir ('*. jpg ');
The file name of file I can be expressed
Filename (I). Name
Number of Files: length (filename)
Method 2:
First, use the following command in msdos(command line) of windows to generate a list.txt file:
Dir path/folder/on/B/S> path/list.txt
Example: Dir D:/test/on/B/S> D:/list.txt
Then use the following in MATLAB:
Filename = textread (sfilefullname, '% s ');
Read all file names to the List cell matrix, and finally obtain each file name for filename {I.
(2) read the data of the file name and store it:
Assume that the data of each file is M * n, then:
Code:
K = length (filename );
Data = zeros (m, n, k );
For II = 1: K
Data (:,:, ii) = yourreadstyle (filename {II });
% Yourreadstyle is the function of the corresponding file reading method.
End
2.
Read data from multiple files consecutively and store the data in multiple matrices (named by file name:
Assuming that the data of each file is M * n, the second file name reading method is used as an example:
Code:
K = length (filename );
For II = 1: K
D =
Yourreadstyle (filename {II });
Eval_r (['data _ ', num2str (II),' =
D; ']);
End
3. File Name naming:
The file name is abc00001, abc00002,... abc00009, abc00010 ,...
Abc00099, abc00100,... abc00879. prepare to put these file names into an array.
Answer:
Code:
A = cell (879,1 );
For k = 1: 879
A {k} =
Sprintf ('%. 5D', k );
End
4.
Automatic Identification of the above various file formats and types: regular expressions can be used for processing to make it more universal. For example, you can use the following code to automatically process the situations from Example 1 to Example 5. However, due to the existence of automatic judgment, the efficiency of some examples (such as Example 1) is naturally lower, for other examples (such as Example 3 and Example 5), the efficiency is estimated to be a little higher (less than a loop ).
Code:
Function
[Data] = distilldata_eight (infile)
% Function description:
% Read the value data in the original file that saves the data into a data variable (automatically determines the data row)
% Instructions:
%
Infile -- original data file name;
% DATA = data variable
Tmpfile = 'tmp2. Mat ';
Fidin = fopen (infile, 'R'); % open the original data file (. List)
Fidtmp = fopen (tmpfile, 'w'); % create and save the data file (excluding instructions)
While ~ Feof (fidin) % determines whether it is the end of the file
Tline = fgetl (fidin); %
Read a line of text from a file (excluding the Enter key)
If ~ Isempty (tline) % determines whether the row is empty
STR = '[^ 0-9 |/. | /-
|/S | E] '; % Regular Expression: whether the row contains any other characters except the-. E number and white space characters
Start =
Regexp (tline, STR, 'one ');
If isempty (start)
Fprintf (fidtmp, '% S/N', tline );
End
End
End
Fclose (fidin );
Fclose (fidtmp );
Data = textread (tmpfile );
Delete (tmpfile)
5. Reading a large amount of data:
You can consider using cyclic batch reading (especially when data is independent) or sparse matrix (For details, refer to the essence of this version:
[Original] experience in improving MATLAB running speed and saving space (3 )). In addition, you can refer to the first chapter of the book "in-depth introduction to MATLAB 7_x hybrid programming ".
6. Read the content of the entire TXT file (obtain all characters in the file ):
Code:
F = fopen('yourfilename.txt ', 'rt'); % t attribute can be omitted as needed
X =
Fread (F, '* char ');
Fclose (f );
7. Save the matrix with different dimensions and its variable names to a TXT file, for example, a1 = 123; a2 = [1 2 3; 4 5 6 ].
The file is as follows:
Quote:
A1:
123
A2:
1 2 3
4 5 6
If it is simpler to write data, you can use the following method, but it is troublesome to read data:
Code:
A1 = 123;
A2 = [1 2 3; 4 5 6];
FID = fopen('myfile.txt ', 'wt ');
For
I = 1:2
Fprintf (FID, '% s:/n % S/N', ['A', int2str (I)],
Mat2str (eval_r (['A', int2str (I)]);
End
Fclose (FID );
On the contrary, if the write process is more complex, the read process is simpler:
Code:
A1 = 123;
A2 = [1 2 3; 4 5 6];
FID = fopen('myfile.txt ', 'wt ');
For
I = 1:2
Fprintf (FID, '% s:/N', ['A', int2str (I)]);
B =
Eval_r (['A', int2str (I)]);
Fprintf (FID, [repmat ('% d', 1, size (B, 2 )),
'/N'], B ');
End
Fclose (FID );