using findstr function for scanning strings in text file.

2 ビュー (過去 30 日間)
sermet
sermet 2015 年 4 月 15 日
編集済み: Guillaume 2015 年 4 月 16 日
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.

採用された回答

Guillaume
Guillaume 2015 年 4 月 15 日
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 件のコメント
Guillaume
Guillaume 2015 年 4 月 16 日
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?)

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by