I data has unnecessary spaces and lines

1 回表示 (過去 30 日間)
Joseph
Joseph 2013 年 6 月 20 日
I have a text file with labels and numeric data. This file reads similar to:
46
Carbon - cs
C -3.4 2.7 1.6
C 1.3 4.6 1.2
C 1.3 -4.2 3.5
O 2.4 4.4 5.6
O -1.0 2.5 9.3
O 2.3 -4.0 2.2
H 2.3 3.5 7.6
H 5.4 -3.6 4.0
H -3.4 -4.5 -6.9
46
Carbon - cs
C -3.4 2.7 1.6
C 1.3 4.6 1.2
C 1.3 -4.2 3.5
O 2.4 4.4 5.6
O -1.0 2.5 9.3
O 2.3 -4.0 2.2
H 2.3 3.5 7.6
H 5.4 -3.6 4.0
H -3.4 -4.5 -6.9
46
Carbon - cs
C -3.4 2.7 1.6
C 1.3 4.6 1.2
C 1.3 -4.2 3.5
O 2.4 4.4 5.6
O -1.0 2.5 9.3
O 2.3 -4.0 2.2
H 2.3 3.5 7.6
H 5.4 -3.6 4.0
H -3.4 -4.5 -6.9
I need to keep every line that starts with H and delete the rest of the lines. I then need to remove the H in front of the remaining lines. I tried using the following code to do so.
infile = fileread('Test_Data_2.txt');
newfile = regexprep(infile, '^[^H].*$', '', 'lineanchors', 'dotexceptnewline');
newfile = strrep(newfile, 'H ', '');
dlmwrite('md_msd.out', newfile, 'delimiter', '\t', ...
'precision', 6)
This code outputs the data I need but it leaves blank lines where the deleted lines used to be. This code also places a space between each number in my data set. So my output looks something like this:
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
2 . 3 3 . 5 7 . 6
5 . 4 - 3 . 6 4 . 0
- 3 . 4 - 4 . 5 - 6 . 9
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
2 . 3 3 . 5 7 . 6
5 . 4 - 3 . 6 4 . 0
- 3 . 4 - 4 . 5 - 6 . 9
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
(blank line here)
2 . 3 3 . 5 7 . 6
5 . 4 - 3 . 6 4 . 0
- 3 . 4 - 4 . 5 - 6 . 9
My data should look something like this:
2.3 3.5 7.6
5.4 -3.6 4.0
-3.4 -4.5 -6.9
2.3 3.5 7.6
5.4 -3.6 4.0
-3.4 -4.5 -6.9
2.3 3.5 7.6
5.4 -3.6 4.0
-3.4 -4.5 -6.9
I'm really not sure why my data is being written into the my text file this way. Can anybody help me?

採用された回答

dpb
dpb 2013 年 6 月 21 日
編集済み: dpb 2013 年 6 月 21 日
I'm not regexp whiz so I'd do sotoo..
>> type joe.dat
46
Carbon - cs
C -3.4 2.7 1.6
C 1.3 4.6 1.2
C 1.3 -4.2 3.5
O 2.4 4.4 5.6
O -1.0 2.5 9.3
O 2.3 -4.0 2.2
H 2.3 3.5 7.6
H 5.4 -3.6 4.0
H -3.4 -4.5 -6.
>> fid=fopen('joe.dat','rt');
>> d=[];
>> while ~feof(fid)
l=fgetl(fid);
if~isempty(l)
if l(1:1)=='H'
d=[d;str2num(l(2:end))];
end
end
end
>> fid=fclose(fid);
>> d
d =
2.3000 3.5000 7.6000
5.4000 -3.6000 4.0000
-3.4000 -4.5000 -6.0000
>> fid=fclose(fid);
  1 件のコメント
Joseph
Joseph 2013 年 6 月 24 日
Perfect! All I had to do was add a dlmwrite line to print the data. Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by