How to find the lines of two words repeated many times in a .txt file
Mostrar comentarios más antiguos
I'm trying to find a two words (DATA & END ) repeated many times in a .txt file how I can get their positions or in any lines they are exist because I want to bring the numbers which are between these two words
this is a part of the file
-- Coefficient of friction (for calculation of temperature) -- SchmierTyp 0:Oil 1:Grease 2:Dry :TABLE FUNCTION ReibKoef INPUT X SchmierTyp TREAT
DATA
1 2 3
0,01 0,02 0,03
END
-- Coefficient of friction (for calculation of temperature) -- SchmierTyp 0:Oil 1:Grease 2:Dry :TABLE FUNCTION ReibKoef INPUT X SchmierTyp TREAT
DATA
1 2 3
0,01 0,02 0,03
END
and I'm trying to do this by typing
haystack = fopen('h1.dat','r');
needle = 'DATA';
line = 0;
found = false;
while ~feof(haystack)
tline = fgetl(haystack);
line = line + 1;
if ischar(tline) && ~isempty(strfind(tline, needle))
found = true;
break;
end
end
if ~found
line = NaN;
end
Thanks in advance
2 comentarios
Guillaume
el 23 de Oct. de 2014
At first glance, your code looks correct. It will find the first line that contains 'DATA'. So what exactly is the problem you are having?
Abdelhadi To'ma
el 23 de Oct. de 2014
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Physical Units en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!