Lau*_*eer 1 string search matlab
我有一个相当大的文本文件,我正在尝试搜索一个特定的术语,以便我可以在那之后开始一个过程,但这对我来说似乎不起作用:
fileID = fopen(resfile,'r');
line = 0;
while 1
tline = fgetl(fileID);
line = line + 1;
if ischar(tline)
startRow = strfind(tline, 'OptimetricsResult');
if isfinite(startRow) == 1;
break
end
end
end
Run Code Online (Sandbox Code Playgroud)
我得到的答案是9,但我的文本文件:
$begin '$base_index$'
$begin 'properties'
all_levels=000000000000
time(year=000000002013, month=000000000006, day=000000000020, hour=000000000008, min=000000000033, sec=000000000033)
version=000000000000
$end 'properties'
$begin '$base_index$'
$index$(pos=000000492036, lin=000000009689, lvl=000000000000)
$end '$base_index$'
Run Code Online (Sandbox Code Playgroud)
肯定没有在前9行?
如果我按Ctrl + F文件,我知道OptimetricsResult只出现一次,并且它是6792行
有什么建议?
谢谢
我认为你的脚本在某种程度上有效,你只是在看错误的变量.我认为你得到的答案是,startRow = 9而不是line = 9.检查变量line.顺便说一句,请注意您没有检查文件结束,因此您的while循环可能会无限期地运行,该文件不包含您的搜索字符串.
另一种方法(在我看来简单得多)将是一次读取所有行(每个行作为单独的字符串存储)textscan,然后应用regexp或strfind:
%// Read lines from input file
fid = fopen(filename, 'r');
C = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
%// Search a specific string and find all rows containing matches
C = strfind(C{1}, 'OptimetricsResult');
rows = find(~cellfun('isempty', C));
Run Code Online (Sandbox Code Playgroud)