2011-12-12 224 views
1

我想搜索文本文件中的特定單詞並返回其位置。此代碼讀取文本罰款...在Matlab中搜索文本文件中的特定單詞

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

但是當我添加此代碼

U = strfind(tline, 'Term'); 

它雖然串'Term'文件中存在,則返回[]

你能幫我嗎?

+3

你要添加哪行代碼?你可以發佈你的代碼與該行? – Blender

+2

請記住,如果您的行中包含'term'的行不包含'term',則U會被[]覆蓋。 –

回答

1

對於我來說,它工作正常:

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

ans = 

    9 

你肯定「期限」真的是你的行?

+0

是的,我確定,但它確定我決定去另一個方向 –

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 
相關問題