Using regexp to Search Large Text File for Wanted Data

21 views (last 30 days)
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.

Accepted Answer

Guillaume
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
Zachary  Parra
Zachary Parra on 7 Oct 2017
Thank you for the comprehensive answer. This will be extremely helpful to look back to in the future.
Cedric
Cedric on 7 Oct 2017
Edited: Cedric on 7 Oct 2017
My pleasure!
(Last edit @ 21:43 UTC)

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!