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?
Best Answer