extract useful info from text file filled with irrelevant info

4 views (last 30 days)
The text files that I have are like this: useless info... * useful date (within useless info) useful info *useless info and like that until the end - if there a way of extracting the useful stuff rather than copy and pasting each individual one (as each file has like 200 things to copy into excel) ? Thanks
dpb on 29 Jul 2016
Edited: dpb on 29 Jul 2016
Sure, but you probably don't need to in order to extract the data of interest.
It would really, Really, REALLY help if you would include an actual file section if you want actual answers instead of continued generalities, but look at the code I provided another poster just recently parsing a similar kind of file to see how to use what's in the file to locate sections of interest... <import-from-data-from-messy-text-file>. That particular file had the nicety of having the number of cases as a parseable value early on in the file so could use a counted outside loop; if, as I gather, your file wouldn't have such just use a while ~feof(fid) loop or equivalent instead. Also, of course, you'd have to keep track/discern which is the one of interest initially and either skip one first or vice versa, depending on the order of whether it's the even/odd case you're interested in. But, as the above shows, it's really pretty simple concept, just takes some consideration of what it is that can be looked for.

Sign in to comment.

Accepted Answer

Guillaume on 6 Aug 2016
Edited: Guillaume on 6 Aug 2016
Now that we finally know what useful and useless look like, we can finally answer the question (mostly).
Here is one way to extract the LPR section(s):
filecontent = fileread('example2.txt');
filesections = regexp(filecontent, 'File name.*?(?=(File name)|$)', 'match'); %match 'File name' and everything that follows up to the next 'File name' or the end of string.
testtypes = regexp(filesections, '(?<=Test type\s*)\S+', 'match', 'once'); %match non-blank characters after 'Test type'
wantedsections = filesections(strcmp(testtypes, 'LPR'));
edit: missing ) in first regex
Jamie Shelley
Jamie Shelley on 7 Aug 2016
This has been the longest weekend ever, but it's finally done (more or less). Thanks for the help with the really technical stuff, I had no idea how to use regexp and that stuff.

Sign in to comment.

More Answers (1)

Shameer Parmar
Shameer Parmar on 29 Jul 2016
use this command..
Data = textread('FileName.txt', '%s', 'delimiter', '');
then apply the logic (FOR and IF loop) according to your requirement for reading and storing the data..
Please provide the data of your text file and about the required data so that I can help you for logic..
Jamie Shelley
Jamie Shelley on 6 Aug 2016
Sorry, I didn't have a copy of it on me, But I've got one now - it's just basically that but continuously for however many experiments were done - Thanks

Sign in to comment.


Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!