Using regexp to Search Large Text File for Wanted Data
21 views (last 30 days)
Show older comments
Hello all! I am attempting to utilize regexp to extract wanted items from text files. Attached below is a sample text file. I wish to extract the start date, EL HGT and Northing (Y)/Easting (X) for UTM (Zone 15). I will eventually have a series of 365 of these text files compressed into one file. Is regexp the best method and how would it be coded? Thanks.
0 Comments
Accepted Answer
Guillaume
on 5 Oct 2017
is regexp the best method?
For EL HGT, probably. For your Northing/Easting for UTM(Zone 15), no because extracting that value involve crossreferencing rows and columns. For that you would have to parse the whole file, and you would have to write your own parser as none of matlab built-in parsers (textscan, etc.) can parse a file that complex as is.
However, if UTM(Zone 15) is always the first column of number for Northing/Easting, then yes you could use a regexp:
filecontent = fileread('sample.txt');
el_hgt_full = regexp(filecontent, '(?<=EL HGT:\s*)[^\n\r]*', 'match', 'once');
el_hgt = str2double(regexp(el_hgt_full, '[+-]?\d+(\.\d+)?', 'match'));
northing = str2double(regexp(filecontent, '(?<=Northing \(Y\) \[meters\]\s*)[+-]?\d+(\.\d+)?', 'match', 'once'));
easting = str2double(regexp(filecontent, '(?<=Easting \(X\) \[meters\]\s*)[+-]?\d+(\.\d+)?', 'match', 'once'));
6 Comments
More Answers (0)
See Also
Categories
Find more on String Parsing 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!