Extracting certain lines from a text file containing both characters and numbers

9 次查看(过去 30 天)
I am trying to load the following data into MATLAB. I then want to extract the line starting with "length area (&)" until "16 0.9 " into a new text file. I have a large number of files exactly like this but where the only difference is the values. Is there a piece of code I could use to extract these lines that can be used for different files? I am pretty new to MATLAB so suggestions welcome.
volume(ml) 122.278 6.064 -- 9.460 6.064
Regional Enhancement (AHA segmentation)
rel.
Data starting
length area (&)
1 0.000
2 0.3
3 0.3
4 158
5 57
6 17
7 0.00
8 0.02
9 13
10 1.15
11 10.20
12 3.68
13 18
14 16
15 0.6
16 0.9
Area of lifetime isotope (all slices)
rel.
slice ending
slice place. segment volume (%)
10 -3.37 * --
20 -4.37 * --
30 -5337 * --
40 -6.37 1 0.0
  2 个评论
dpb
dpb 2019-8-30
I took a guess at formatting the code/text...don't know where actual line breaks are for sure, however, so don't know how many header lines are actually in the file.
Always helps to actually attach a sample file instead of trying to paste in if is possibility of line wrap.
Guillaume
Guillaume 2019-8-30
Rather than pasting the content of the text file, attach the file to the question. Text files can have different text encodings and line ending characters which are all lost when you paste but may make a difference to the parsing.
Plus it makes your comment wayyyyy too long (please delete the paste after attaching your file).

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2019-8-30
Presuming records (including blank lines) are as above,
fid=fopen('HB.txt');
data=cell2mat(textscan(fid,'%f%f','headerlines',5));
fid=fclose(fid);
returned
data =
1.0000 0
2.0000 0.3000
3.0000 0.3000
4.0000 158.0000
5.0000 57.0000
6.0000 17.0000
7.0000 0
8.0000 0.0200
9.0000 13.0000
10.0000 1.1500
11.0000 10.2000
12.0000 3.6800
13.0000 18.0000
14.0000 16.0000
15.0000 0.6000
16.0000 0.9000
May need to adjust the number of header lines; when I copied from the text above, the apparent blank records disappeared so don't know if they're real or not in the original.
Using the specific format causes textscan to fail on the first record it finds that doesn't match which is the next after the last data you happen to be interested in--serendipity since on failure it returns the data that it did successfully convert.
  4 个评论
dpb
dpb 2019-8-30
The above answer still works, you just have to adjust the 'headerlines' argument to account for how much stuff there is before the section of interest.
If the file format is always identical, you can just use a constant empirically determined by loading a file in the editor and looking.
If there can be a different number of lines in preceding data sections, then you'll have to read through the file until you find that header line preceding the section of interest, then read from that point using the correct 'headerlines' argument count for just the extra lines at that point.
I posted a solution to another question within last couple weeks at outside that illustrates identically this--unfortunately, I can't recall the actual Q? title to point at it immediately, but it is essentially just
fid=fopen('HB.txt');
while ~feof(fid)
l=fgetl(fid);
if strfind(l,'yoursearchpattern'),break,end
end
data=cell2mat(textscan(fid,'%f%f','headerlines',N));
fid=fclose(fid);
where you need to pick the unique search pattern and N lines to skip after the section is found.
Stephen23
Stephen23 2019-8-30
编辑:Stephen23 2019-8-30
"Any suggestions?"
Upload data files, rather than pasting them as text in your questions/comments.
"apologies for the length of text"
Files can be uploaded by clicking the paperclip button.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by