I data has unnecessary spaces and lines
1 view (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
dpb
on 21 Jun 2013
Edited: dpb
on 21 Jun 2013
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);
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!