search and delete text lines when certain strings are missing

6 views (last 30 days)
Hi everyone, I have a text strings. This is just a little portion of the file but the original is much larger. Sometimes I get a row were something went wrong, like the third row of this example just below. On the third string part of the coordinates are missing, sometime part of the Northings, sometimes part of the Eastings, sometimes both. The rows that are right do not always have the same length. So I was thinking to look for those rows where either the 'N' letter or the 'E' letter are missing. I tried the 'strfind' and 'isempty' commands but I am not managing to get it right, it returns the same data as before. Can someone please help me??????? Thank you.

Answers (2)

Cedric Wannaz
Cedric Wannaz on 11 Jul 2013
Edited: Cedric Wannaz on 11 Jul 2013
If there cannot be any N or E other than those for Northings and Eastings, you can proceed as follows:
fh = fopen('myData.txt', 'r') ;
while ~feof(fh)
line = fgetl(fh) ;
if isempty(strfind(line, 'N')) || isempty(strfind(line, 'E'))
continue ;
% Valid line, treat it (here, we just print it for the example).
fprintf('%s\n', line) ;
fclose(fh) ;
If N or E can appear elsewhere, and it seems to be the case as in line #2, one option could be to count commas, e.g.
fh = fopen('myData.txt', 'r') ;
while ~feof(fh)
line = fgetl(fh) ;
if sum(line == ',') < 14
% Valid line, treat it.
fprintf('%s\n', line) ;
But how are you treating valid lines afterwards? If they can have various formats and you need pattern matching, REGEXP could be an optimal solution. If they have a rather fixed format, SSCANF/TEXTSCAN/etc will certainly work well.

Muthu Annamalai
Muthu Annamalai on 10 Jul 2013
Edited: Muthu Annamalai on 10 Jul 2013
Have you tried regexp ? regexp
My recommendation is to use regexp split at ',' and process the resulting cell-array.


Find more on Characters and Strings 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!