using findstr function for scanning strings in text file.

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.

 Akzeptierte Antwort

Guillaume
Guillaume am 15 Apr. 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 Kommentare

sermet
sermet am 15 Apr. 2015
Bearbeitet: sermet am 15 Apr. 2015
I need to stop the loop when there is no any other # / TYPES OF OBSERV then I can see the lines which involve all # / TYPES OF OBSERV
Guillaume
Guillaume am 15 Apr. 2015
Bearbeitet: Guillaume am 16 Apr. 2015
Ok, I misunderstood. You have two options:
1) using strfind, don't break out of the loop when you've found your string. Do whatever you need to do and carry on with the loop. Only break out of the loop when you encounter the end of file
fide = fopen('rinex.txt')
while true
line = fgetl(fide)
if ~ischar(tline) %eof
break;
end
if ~isempty(strfind(line,'# / TYPES OF OBSERV'))
%do whatever you need to do with the line and carry on
end
end
2) read the whole file and use a regular expression to extract the lines of interest
filecontent = fileread('rinex.txt');
observations = regexp(filecontent, '^.*(?=# / TYPES OF OBSERV\s*)$', 'match', 'lineanchors', 'dotexceptnewline');
the above regular expression will return the content of each line that ends with '# / TYPES OF OBSERV'
edit: added 'dotexceptnewline' to regex and match for blank spaces
hi,
The second codes produces observations =
{}
the first one just read the entire file and gives
line =
-1
Guillaume
Guillaume am 16 Apr. 2015
Bearbeitet: Guillaume am 16 Apr. 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?)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 15 Apr. 2015

Bearbeitet:

am 16 Apr. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by