我想在文本文件中搜索特定单词并返回其位置。此代码读取文本正确...
fid = fopen('jojo-1 .txt','r');
while 1
tline = fgetl(fid);
if ~ischar(tline)
break
end
end
但是当我添加此代码时
U = strfind(tline, 'Term');
它会返回[]
,尽管文件中存在字符串'Term'
。
你能帮帮我吗?
答案 0 :(得分:1)
对我来说,它运作良好:
strfind(' ertret Term ewrwerewr', 'Term')
ans =
9
你确定'Term'确实在你的队伍中吗?
答案 1 :(得分:0)
我相信你的~ischar(tline)
会造成麻烦,因为当tline
不是char时,代码“中断”所以strfind
无法找到任何内容
因此我所做的市长改变实际上是在被识别为具有某些字符的行的行中搜索字符串。
我尝试在我的TEXT文件上稍微修改一下代码:
yyyy/mmdd(or -ddd)/hh.h):2011/-201/10.0UT geog Lat/Long/Alt= 50.0/ 210.0/2000.0
NeQuick is used for topside Ne profile
URSI maps are used for the F2 peak density (NmF2)
CCIR maps are used for the F2 peak height (hmF2)
IRI-95 option is used for D-region
ABT-2009 option is used for the bottomside thickness parameter B0
The foF2 STORM model is turned on
Scotto-97 no L option is used for the F1 occurrence probability
TBT-2011 option is used for the electron temperature
RBY10+TTS03 option is used for ion composition
Peak Densities/cm-3: NmF2= 281323.9 NmF1= 0.0 NmE= 2403.3
Peak Heights/km: hmF2= 312.47 hmF1= 0.00 hmE= 110.00
Solar Zenith Angle/degree 109.6
Dip (Magnetic Inclination)/degree 65.76
Modip (Modified Dip)/degree 55.06
Solar Sunspot Number (12-months running mean) Rz12 57.5
Ionospheric-Effective Solar Index IG12 63.3
TEC [1.E16 m-2] is obtained by numerical integration in 1km steps
from 50 to 2000.0 km. t is the percentage of TEC above the F peak.
-
H ELECTRON DENSITY TEMPERATURES ION PERCENTAGES/% 1E16m-2
km Ne/cm-3 Ne/NmF2 Tn/K Ti/K Te/K O+ N+ H+ He+ O2+ NO+ Clust TEC t/%
0.0 -1 -1.000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 7.7 75
5.0 -1 -1.000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 7.7 75
10.0 -1 -1.000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 7.7 75
它是一个电离层模型的输出,但这并不重要:)
所以我使用以下Matlab代码来查找它的字符串 TEMPERATURES
out = fopen('fort.7'); % Open function
counter = 0; % line counter (sloppy but works)
while 1 % infinite loop
tline = fgetl(out); % read a line
counter = counter + 1; % we are one line further
if ischar(tline) % if the line is string
U = strfind(tline, 'TEMPERATURES'); % where the string start (if at all)
if isfinite(U) == 1; % if it is a number actually
break % we found it, lets go home
end
end
end
结果:
counter = 26
U = 27