2011-12-12 3 views
1

Je souhaite rechercher un mot spécifique dans un fichier texte et renvoyer sa position. Ce code lit l'amende texte ...rechercher un mot spécifique dans un fichier texte dans Matlab

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

mais quand j'ajoute ce code

U = strfind(tline, 'Term'); 

retourne [] bien que la chaîne 'Term' existe dans le fichier.

Pouvez-vous s'il vous plaît aidez-moi?

+3

Où ajoutez-vous cette ligne de code? Pouvez-vous poster votre code avec cette ligne? – Blender

+2

Gardez à l'esprit que si vous avez des lignes après votre ligne avec 'term' dans lequel ne contient pas 'term', U sera écrasé avec []. –

Répondre

1

Pour moi, il fonctionne très bien:

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

ans = 

    9 

Êtes-vous sûr que « terme » est vraiment dans votre ligne?

+0

oui je suis sûr mais c'est ok j'ai décidé d'aller dans une autre direction –

0

Je crois que votre ~ischar(tline) fait la peine parce que le code « casse » lorsque le tline n'est pas char..so le strfind ne peut pas trouver quoi que ce soit. Donc le changement de maire que j'ai fait est de rechercher réellement la chaîne à la ligne qui a été identifiée comme une ligne avec quelques caractères.

J'ai essayé une petite modification de peu de votre code sur mon fichier texte:

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 

il est une sortie d'un modèle ionosphérique mais ce n'est pas important :)

donc j'ai utilisé suivant le code Matlab pour trouver où il chaîne TEMPÉRATURES

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 

résultats:

counter = 26 
U = 27 
Questions connexes