search and delete text lines when certain strings are missing

3 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.
'$GPGLL,5341.65350044,N,00713.34866656,E,104325.00,A,D*64,$SDDBT,00023.2,f,0007.1,M,0003.8,F*08'
'$GPGLL,5341.65358961,N,00713.34455216,E,104327.00,A,D*6C,$SDDBT,00023,M,00.0,N,,K*4A'
'$GPGLL,5341.65523146A,D*68,$SDDBT,00023.2,f,0007.1,M,0003.8,F*08'
'$GPGLL,5341.65527973,N,00713.29472566,E,104351.00,A,D*69,$SDDBT,00023.2,f,0007.1,M,0003.8,F*08'
'$GPGLL,5341.65542543,N,00713.28847321,E,104354.00,A,D*6E,$SDDBT,00023.2,f,0007.1,M,0003.8,F*08'
'$GPGLL,5341.65550430,N,00713.28218422,E,104357.00,A,D*6F,$SDDBT,00022.9,f,0007.0,M,0003.8,F*03'
$GPGLL,5341.71916345,E,105127.00,A,D*6E,$SDDBT,00032.8,f,0010.0,M,0005.4,F*0F'
'$GPGLL,5341.65570054,N,00713.27593477,E,104400.00,A,D*65,$SDDBT,00022.9,f,0007.0,M,0003.8,F*03'
'$GPGLL,5341.65608654,N,00713.26774575,E,104404.00,A,D*62,$SDDBT,00022.9,f,0007.0,M,0003.8,F*03'

Answers (2)

Cedric
Cedric on 11 Jul 2013
Edited: Cedric 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 ;
end
% Valid line, treat it (here, we just print it for the example).
fprintf('%s\n', line) ;
end
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
continue
end
% Valid line, treat it.
fprintf('%s\n', line) ;
end
fclose(fh)
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.

Categories

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!