Read a text file with varying number of colums
22 次查看(过去 30 天)
显示 更早的评论
I am trying to read a text file with varying number of columns, such as this:
#5:v3.0_ST:1:2631.1301,N:140.0:081.000:12.5;
#5:v3.0_ST:1:2631.1301,N:111.4:100.000:12.5:18.7:32.3;
#5:v3.0_ST:1:2631.1299,N:111.5:101.000:12.5:18.7:32.3;
#5:v3.0_ST:1:2631.1315,N:136.4:082.000:12.3;
#5:v3.0_ST:1:2631.1334,N:132.8:083.000:12.4;
The data is delimited by " : " (colon). I understand, there is some way of doing this using textscan, but I do not know how to do it for varying columns. Can someone give me a hint?
Thanks
采纳的回答
per isakson
2015-1-12
编辑:per isakson
2015-1-12
Try
fid = fopen( 'cssm.txt' );
cac = textscan( fid, '%s%s%s%s%s%s%s%s%s', 'CollectOutput' ...
, true, 'Delimiter', ':;' );
[~] = fclose( fid );
it returns with R2013a
>> cac{:}
ans =
Columns 1 through 8
'#5' 'v3.0_ST' '1' '2631.1301,N' '140.0' '081.000' '12.5' ''
'#5' 'v3.0_ST' '1' '2631.1301,N' '111.4' '100.000' '12.5' '18.7'
'#5' 'v3.0_ST' '1' '2631.1299,N' '111.5' '101.000' '12.5' '18.7'
'#5' 'v3.0_ST' '1' '2631.1315,N' '136.4' '082.000' '12.3' ''
'#5' 'v3.0_ST' '1' '2631.1334,N' '132.8' '083.000' '12.4' []
Column 9
''
'32.3'
'32.3'
''
[]
>>
 
Comments:
- importdata reads and parses this file in R2013a too. However, textscan is significantly faster.
- the empty, "[]", at the right bottom corner must be handled separately
- the format string should be modified to account for the numberical columns
- I don't think textscan would have behaved this nice some years ago.
 
Addendum
This might better match what you look for
fid = fopen( 'cssm.txt' );
cac = textscan( fid, '%s%s%f%f%s%f%f%f%f%f' ...
, 'CollectOutput' , true ...
, 'Delimiter' , ':;,' );
[~] = fclose( fid );
Output:
>> cac{:}
ans =
'#5' 'v3.0_ST'
'#5' 'v3.0_ST'
'#5' 'v3.0_ST'
'#5' 'v3.0_ST'
'#5' 'v3.0_ST'
ans =
1.0e+03 *
0.0010 2.6311
0.0010 2.6311
0.0010 2.6311
0.0010 2.6311
0.0010 2.6311
ans =
'N'
'N'
'N'
'N'
'N'
ans =
140.0000 81.0000 12.5000 NaN NaN
111.4000 100.0000 12.5000 18.7000 32.3000
111.5000 101.0000 12.5000 18.7000 32.3000
136.4000 82.0000 12.3000 NaN NaN
132.8000 83.0000 12.4000 NaN NaN
>>
0 个评论
更多回答(1 个)
Aditya Dua
2015-1-11
I tried this on the file segment you posted and it worked:
inp = importdata('file.txt'); where inp(k) contains the k^th line of the text file.
I'm using MATLAB R2014b
Aditya
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!