在Matlab中搜索文本文件中的特定单词
问题描述:
我想搜索文本文件中的特定单词并返回其位置。此代码读取文本罚款...在Matlab中搜索文本文件中的特定单词
fid = fopen('jojo-1 .txt','r');
while 1
tline = fgetl(fid);
if ~ischar(tline)
break
end
end
但是当我添加此代码
U = strfind(tline, 'Term');
它虽然串'Term'
文件中存在,则返回[]
。
你能帮我吗?
答
对于我来说,它工作正常:
strfind(' ertret Term ewrwerewr', 'Term')
ans =
9
你肯定“期限”真的是你的行?
+0
是的,我确定,但它确定我决定去另一个方向 –
答
我相信你~ischar(tline)
使麻烦,因为代码“休息”当tline
不char..so的strfind
什么也找不到。
因此,我所做的市长更改实际上是在被识别为具有某些字符的行的行上搜索字符串。
我想在我的文本文件,你的代码一点点修改:
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代码要找到它串下
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
你要添加哪行代码?你可以发布你的代码与该行? – Blender
请记住,如果您的行中包含'term'的行不包含'term',则U会被[]覆盖。 –