using findstr function for scanning strings in text file.

4 visualizaciones (últimos 30 días)
sermet
sermet el 15 de Abr. de 2015
Editada: Guillaume el 16 de Abr. de 2015
2.11 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE
teqc 2014Jan16 NOAA/NOS/NGS/CORS 20150102 00:26:47UTCPGM / RUN BY / DATE
Solaris x86 5.10|AMD64|cc SC5.8 -xarch=amd64|=+|=+ COMMENT
teqc 2014Jan16 NOAA/NOS/NGS/CORS 20150102 00:15:53UTCCOMMENT
BIT 2 OF LLI FLAGS DATA COLLECTED UNDER A/S CONDITION COMMENT
BRMU MARKER NAME
42501S004 MARKER NUMBER
Giovanni Sella NGS OBSERVER / AGENCY
351248 LEICA GRX1200GGPRO 8.71/3.823 REC # / TYPE / VERS
00480 JAVRINGANT_DM NONE ANT # / TYPE
2304703.4760 -4874817.1770 3395186.9500 APPROX POSITION XYZ
0.0000 0.0000 0.0000 ANTENNA: DELTA H/E/N
1 1 WAVELENGTH FACT L1/2
10 L1 L2 L5 C1 C2 P2 C5 S1 S2# / TYPES OF OBSERV
S5 # / TYPES OF OBSERV
30.0000 INTERVAL
%Above is the rinex.txt file. I need to classify how many # / TYPES OF OBSERV string exist. The below code works proper in text file if only 1 line includes # / TYPES OF OBSERV.
fide = fopen('rinex.txt')
head_lines = 0
while 1
head_lines = head_lines+1
line = fgetl(fide)
answer = findstr(line,'# / TYPES OF OBSERV')
if ~isempty(answer)
break;
end
end
%The second # / TYPES OF OBSERV doesn't appear in line.

Respuesta aceptada

Guillaume
Guillaume el 15 de Abr. de 2015
Of course, your code only finds the 1st occurence of the string. You've designed it this way:
  • read one line at a time ( fgetl(fide))
  • find the occurrences of the string in the line ( strfind(line,...)
  • if an occurrence found stop reading ( if ... break;)
If you want to find all the occurrences in the whole file, then read the whole file at once:
filecontent = fileread('rinex.txt');
noccurrences = numel(strfind(filecontent, '# / TYPES OF OBSERV'));
  4 comentarios
sermet
sermet el 15 de Abr. de 2015
hi,
The second codes produces observations =
{}
the first one just read the entire file and gives
line =
-1
Guillaume
Guillaume el 16 de Abr. de 2015
Editada: Guillaume el 16 de Abr. de 2015
Don't accept an answer until you're satisfied with the solution.
The second codes produces observations = {}
I'd forgotten that matlab's regexp engine by default matches newlines with dot which is the complete opposite behaviour of any other regex engine I know (C++, .Net, php, python, java, ruby, perl ...). Therefore the regular expression needs a 'dotexceptnewline' option.
Secondly, the regex assumes that 'TYPES OF OBSERV' terminates the line with no space afterward. Maybe you have spaces afterward.
I've edited my answer to fix the above two. In any case, you may need to tailor the regular expression to the exact pattern you have.
The first one just read the entire file and gives line = -1
Of course it would, you need to replace the comment %do whatever you need... with whatever code you need to process the line. (store it in a cell array? parse it?)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Install Products en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by