用不同的格式读取一个文本文件到MATLAB [英] reading a text file to MATLAB with different formatting
问题描述
我有一个文本文件,但不幸的是它格式不好,但是我想读取文本文件的内容到一个矩阵,但我不知道该怎么做。
尝试使用 fscanf
, textscan
, textread
,剩下的只是把所有东西都复制到一个单元格中,但我不想这样。
这个内容是这样的:只想读取小数点而不是绝对数字。有人可以帮助我。
1:13.27; 3:20.68; 6:8.271; 7:3.308; 8:8.328;
9:6.655; 10:16.58; 11:9.925; 12:12.41; 13:4.135;
14:9.925; 15:11.58; 16:10.87; 17:1.654; 18:4.962;
19:6.655; 22:10.98; 23:24.25; 24:47.33; 25:11.6;
26:9.925; 27:5.809; 28:5.001; 29:6.617; 30:7.577;
31:9.155; 32:7.444; 33:28.58; 34:9.155; 35:35.83;
只要使用textscan并忽略你不需要的东西需要像数字和:给你一个相当简单的解决方案:
$ $ $ $ $ $ $ c $ fid = fopen('test.txt','rt );
data = textscan(fid,'%* u%* 1s%f','Delimiter',';');
fclose(fid);
将test.txt更改为您的文件名。
data是一个包含双打的单元格。
>>数据{:}
ans =
13.2700
20.6800
8.2710
3.3080
8.3280
6.6550
16.5800
9.9250
12.4100
4.1350
9.9250
11.5800
10.8700
1.6540
4.9620
6.6550
10.9800
24.2500
47.3300
11.6000
9.9250
5.8090
5.0010
6.6170
7.5770
9.1550
7.4440
28.5800
9.1550
35.8300
I have a text file, but unfortunately its been poorly formatted, however i want to read the content of the text file into a a matrix, but I don't know how to do that.
When try to use fscanf
, textscan
, textread
and the rest it just copies everything into one cell, but i don't want it that way.
This how the content looks like: so i want to read only the decimals not the absolute figures. Can someone help me.
1 : 13.27 ; 3 : 20.68 ; 6 : 8.271 ; 7 : 3.308 ; 8 : 8.328 ;
9 : 6.655 ; 10 : 16.58 ; 11 : 9.925 ; 12 : 12.41 ; 13 : 4.135 ;
14 : 9.925 ; 15 : 11.58 ; 16 : 10.87 ; 17 : 1.654 ; 18 : 4.962 ;
19 : 6.655 ; 22 : 10.98 ; 23 : 24.25 ; 24 : 47.33 ; 25 : 11.6 ;
26 : 9.925 ; 27 : 5.809 ; 28 : 5.001 ; 29 : 6.617 ; 30 : 7.577 ;
31 : 9.155 ; 32 : 7.444 ; 33 : 28.58 ; 34 : 9.155 ; 35 : 35.83 ;
Just using textscan and ignoring the things you don't need, like the numbers and : gives you quite a simple solution:
fid = fopen('test.txt', 'rt');
data = textscan(fid, '%*u %*1s %f', 'Delimiter', ';');
fclose(fid);
Change test.txt to your filename. data is a cell with your doubles in it.
>> data{:}
ans =
13.2700
20.6800
8.2710
3.3080
8.3280
6.6550
16.5800
9.9250
12.4100
4.1350
9.9250
11.5800
10.8700
1.6540
4.9620
6.6550
10.9800
24.2500
47.3300
11.6000
9.9250
5.8090
5.0010
6.6170
7.5770
9.1550
7.4440
28.5800
9.1550
35.8300
这篇关于用不同的格式读取一个文本文件到MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!