MATLAB: Using findstr function for scanning strings in text file.

fget1findstr

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.

Best Answer

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'));