【问题标题】:search for specific word in text file in Matlab在 Matlab 中搜索文本文件中的特定单词
【发布时间】:2011-12-12 17:50:52
【问题描述】:

我想在一个文本文件中搜索一个特定的单词并返回它的位置。此代码可以很好地读取文本...

fid = fopen('jojo-1 .txt','r');
while 1
    tline = fgetl(fid);
    if ~ischar(tline)
       break
    end
end

但是当我添加这段代码时

U = strfind(tline, 'Term');

尽管文件中存在字符串'Term',但它返回[]

你能帮帮我吗?

【问题讨论】:

  • 你在哪里添加那行代码?你能用那行发布你的代码吗?
  • 请记住,如果您的行后面有带有 'term' 且不包含 'term' 的行,U 将被 [] 覆盖。

标签: string matlab search file-io


【解决方案1】:

对我来说,它工作正常:

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

ans =

     9

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

【讨论】:

  • 是的,我确定,但没关系,我决定换个方向
【解决方案2】:

我相信您的~ischar(tline) 会造成麻烦,因为当tline 不是字符时,代码“中断”。所以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

【讨论】:

    猜你喜欢
    • 2014-03-10
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2015-01-31
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多