Count occurrence in a text/number file
Résolu
Ocefx64
Messages postés
5
Date d'inscription
Statut
Membre
Dernière intervention
-
yg_be Messages postés 23541 Date d'inscription Statut Contributeur Dernière intervention -
yg_be Messages postés 23541 Date d'inscription Statut Contributeur Dernière intervention -
Hello everybody!
I'm a beginner in MatLab, and I am blocking right now.
I have several files named like that: OutputFile_1, OutputFile_2, ... OutputFile_40410 These files are constructed like that:
2008 5 1 0013 32.5 L -12.845 165.450 35.0 VAN 5 0.1 2.1LVAN 1
GAP=355 0.33 36.9 81.8999.9 0.1882E+04 0.1078E+06 -0.8944E+04E
ACTION:UPD 16-09-24 15:16 OP:ocea STATUS: ID:20080501001332 L I
2008-05-01-0013-55S.VNRC__051_MSEED 6
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
VSARABZ EP 0 014 14.65 91 -0.0610 312 148
VSARABZ IAML 014 19.06 5.90 0.12 312 148
VKOLHBZ EP 2 014 15.86 91 -0.04 5 321 145
VKOLHBZ IAML 014 16.78 9.42 0.10 321 145
VBUTMBZ EP 2 014 16.44 91 0.23 5 324 149
VBUTMBZ IAML 014 16.65 5.91 0.10 324 149
VBUTMBZ ES 3 014 50.35 91 0.01 2 324 149
VIRHOBZ EP 2 014 19.00 91 0.17 5 345 147
VIRHOBZ IAML 014 19.17 5.84 0.12 345 147
VAVUNBZ EP 1 014 20.73 91 -0.05 7 359 150
VAVUNBZ IAML 014 20.94 4.99 0.12 359 150
I would like to read all of these files, count the number of 'EP' and 'ES' occurrences, and if the number of 'EP' + 'ES' is higher than 8, stock the files in on unique file.
The first issue is that I'm not able to count the number of occurrences. I tried something like that:
And I got this Error:
Error using count Search term must be a string array, character vector, or cell array of character vectors.
I tried with sum(fid=='EP') but I just obtained ans = 0.
I have the filling that it's not so complicated, but I'm not good enough in matlab to did it alone.
The aim after is to continue with something like that, I guess:
select_events = fopen('select_events.txt', 'w')
Thank you a lot for your help.
I'm a beginner in MatLab, and I am blocking right now.
I have several files named like that: OutputFile_1, OutputFile_2, ... OutputFile_40410 These files are constructed like that:
2008 5 1 0013 32.5 L -12.845 165.450 35.0 VAN 5 0.1 2.1LVAN 1
GAP=355 0.33 36.9 81.8999.9 0.1882E+04 0.1078E+06 -0.8944E+04E
ACTION:UPD 16-09-24 15:16 OP:ocea STATUS: ID:20080501001332 L I
2008-05-01-0013-55S.VNRC__051_MSEED 6
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
VSARABZ EP 0 014 14.65 91 -0.0610 312 148
VSARABZ IAML 014 19.06 5.90 0.12 312 148
VKOLHBZ EP 2 014 15.86 91 -0.04 5 321 145
VKOLHBZ IAML 014 16.78 9.42 0.10 321 145
VBUTMBZ EP 2 014 16.44 91 0.23 5 324 149
VBUTMBZ IAML 014 16.65 5.91 0.10 324 149
VBUTMBZ ES 3 014 50.35 91 0.01 2 324 149
VIRHOBZ EP 2 014 19.00 91 0.17 5 345 147
VIRHOBZ IAML 014 19.17 5.84 0.12 345 147
VAVUNBZ EP 1 014 20.73 91 -0.05 7 359 150
VAVUNBZ IAML 014 20.94 4.99 0.12 359 150
I would like to read all of these files, count the number of 'EP' and 'ES' occurrences, and if the number of 'EP' + 'ES' is higher than 8, stock the files in on unique file.
The first issue is that I'm not able to count the number of occurrences. I tried something like that:
clc
clear all
close all
Files = dir(OutputFile_*);
Nbr_Event = lenght(Files);
for i = 1:Nbr_Event;
fid = fopen('Files');
occ = cound (fid, 'EP');
end
And I got this Error:
Error using count Search term must be a string array, character vector, or cell array of character vectors.
I tried with sum(fid=='EP') but I just obtained ans = 0.
I have the filling that it's not so complicated, but I'm not good enough in matlab to did it alone.
The aim after is to continue with something like that, I guess:
select_events = fopen('select_events.txt', 'w')
if Occ >= 8;
fprintf (Files, \n)
Thank you a lot for your help.
A voir également:
- Count occurrence in a text/number file
- .Bin file - Guide
- Host file - Guide
- .Dat file - Guide
- Iso file - Guide
- Navigation in private - Guide
1 réponse
yg_be
Messages postés
23541
Date d'inscription
Statut
Contributeur
Dernière intervention
Ambassadeur
1 584
bonjour,
cound ou count?
je me demande si ton programme ne devrait pas lire le contenu du fichier.
par ailleurs, ne devrais-tu pas faire la somme des occurrences dans chacun des fichiers?
cound ou count?
je me demande si ton programme ne devrait pas lire le contenu du fichier.
par ailleurs, ne devrais-tu pas faire la somme des occurrences dans chacun des fichiers?
J'ai essayé de faire la somme, comme je l'ai éxpliqué, mais sans succés pour le moment. Et oui se sera la somme de l'occurrence pour chacun de mes fichiers
je suggère de commencer par un exercice plus simple: lire et afficher le contenu d'un fichier, puis compter le nombre de lignes d'un fichier.
quelle instruction as-tu utilisée pour faire la somme du nombre d’occurrences?
J''ai réussis avec ça :
Events = fileread('Events/OutputFile_2.txt')
occ_pha = count(Events,[" EP "," ES "])
Maintenant j'essaie dans faire une boucle pour que le chiffre 2 change, mais comme c'est dans le nom du fichier, je ne sais pas encore trop comment écrire cela.
repars de ton code de départ:
remarques que tu n'y utilisais pas i.
for i = 2:5 % test jusqu'à 5
filename = sprintf ('Events/%d_OutputFile.txt',i);
Events = fileread(filename);
occ_pha = count(Events,[" EP "," ES "])
end
Ca marche bien
Merci!