MATLAB Answers

extract useful info from text file filled with irrelevant info

2 ビュー (過去 30 日間)
Jamie Shelley
Jamie Shelley 2016 年 7 月 26 日
コメント済み: Jamie Shelley 2016 年 8 月 7 日
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

  13 件のコメント

表示 10 件の古いコメント
dpb
dpb 2016 年 7 月 27 日
In one form or another, yes. "To repeat an operation, it is necessary to repeat an operation!" :)
It's quite possible you could read the whole file into memory and do string comparison to find the row indices of all the headers in "one swell foop" but then you would need to iterate over those altho again one could likely be able to delete large chunks between those locations leaving only the sections of interest to process, but still in the end one would needs must iterate, yes. Now, again, with Matlab you may be able to use things like cellfun or the like and mask the underlying looping at higher levels, but it reduces to a for...end loop under the hood eventually. Often it's far simpler at least initially to just use the "deadahead" approach and use the explicit loop and only after it's shown to be too slow worry excessively about vectorizing. And, often with these more complex cases the vectorized solution actually doesn't outperform the loop and is more difficult to write, debug and maintain.
Jamie Shelley
Jamie Shelley 2016 年 7 月 29 日
Okay thank you - I think I see what you mean, is there a way of finding the number of paragraphs/lines in the file please?
dpb
dpb 2016 年 7 月 29 日
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.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2016 年 8 月 6 日
編集済み: Guillaume 2016 年 8 月 6 日
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

  11 件のコメント

表示 8 件の古いコメント
Jamie Shelley
Jamie Shelley 2016 年 8 月 7 日
I think that will be the last thing I need as I've produced the rest of the code using a sample I made manually, so it's just one small step from finishing but I can't work out how to get it into that format in an xlsx file
Jamie Shelley
Jamie Shelley 2016 年 8 月 7 日
In the excel file, the times at the bottom aren't neccesary as I programmed them in differently, I just need to get the test results into the layout shown in the example.xlsx file (but times there aren't neccessary), is there any way of extracting the tables from single cells into the format in the excel file please? If not, I'll start doing it all manually but I feel like it's so close to being finished that it would be a shame to start doing it manually now. Thanks
Jamie Shelley
Jamie Shelley 2016 年 8 月 7 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Shameer Parmar
Shameer Parmar 2016 年 7 月 29 日
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..

  3 件のコメント

Jamie Shelley
Jamie Shelley 2016 年 8 月 6 日
Thanks, I tried that line of code but couldn't get it to work - I've enclosed a copy of the file (I've removed the irrelevant info, but I need to get the info from the EIS test and not the LPR ), I've been trying to work it out but I'm just having no luck at all with this one. Thanks
Guillaume
Guillaume 2016 年 8 月 6 日
At last! We finally get an example of the data as dpb and I have been asking for ages. You still haven't given us the full picture, but can still make a start at answering the question.
Jamie Shelley
Jamie Shelley 2016 年 8 月 6 日
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

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by