String 在Matlab中搜索文本文件中的特定单词

String 在Matlab中搜索文本文件中的特定单词,string,matlab,search,file-io,String,Matlab,Search,File Io,我想在文本文件中搜索特定单词并返回其位置。这段代码可以很好地读取文本 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

我想在文本文件中搜索特定单词并返回其位置。这段代码可以很好地读取文本

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
你确定“术语”真的在你的行中吗?

对我来说,它很好用:

strfind(' ertret Term ewrwerewr', 'Term')

ans =

     9

你确定“Term”真的在你的行中吗?

我相信你的
~ischar(tline)
会带来麻烦,因为当
tline
不是char时,代码会“断开”。因此
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代码来找到它的位置

结果:

counter = 26
U = 27

我相信你的
~ischar(tline)
会制造麻烦,因为当
tline
不是char时,代码会“中断”。因此
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代码来找到它的位置

结果:

counter = 26
U = 27

你在哪里添加那行代码?你能用那一行发布你的代码吗?请记住,如果你的代码行后面有不包含“term”的带有“term”的行,你将被[]覆盖。你在哪里添加那一行代码?你能用那一行来发布你的代码吗?请记住,如果在你的“term”行后面有不包含“term”的行,你将被[]覆盖。是的,我确定,但没关系,我决定去另一个方向是的,我确定,但没关系,我决定去另一个方向