MATLAB Answers


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

Asked by HB
on 29 Aug 2019
Latest activity Edited by Stephen Cobeldick on 30 Aug 2019
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)
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)
slice ending
slice place. segment volume (%)
10 -3.37 * --
20 -4.37 * --
30 -5337 * --
40 -6.37 1 0.0


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.
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).

Sign in to comment.

1 Answer

Answer by dpb
on 30 Aug 2019

Presuming records (including blank lines) are as above,
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.


Show 1 older comment
That is NOT attaching the file, that's just pasting in more. Use the paperclip icon instead.
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
while ~feof(fid)
if strfind(l,'yoursearchpattern'),break,end
where you need to pick the unique search pattern and N lines to skip after the section is found.
"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.

Sign in to comment.