Count occurrence in a file with text and number

2 visualizaciones (últimos 30 días)
Oce
Oce el 11 de Sept. de 2018
Comentada: Oce el 12 de Sept. de 2018
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:
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.
  2 comentarios
Stephen23
Stephen23 el 12 de Sept. de 2018
@Oce: please do not close question that have answers.
Oce
Oce el 12 de Sept. de 2018
Ok :-)

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 11 de Sept. de 2018
You can use fgetl() and contains() to count the number of times each of those strings occurs. Attach your text file if you tried it and still couldn't figure it out.
  5 comentarios
Image Analyst
Image Analyst el 12 de Sept. de 2018
Glad my suggestion of fileread() worked for you. If the release of MATLAB is R2016b or later, you can use count(), otherwise use strfind() and length(). If the code works, maybe you can "Accept this answer". Thanks in advance.
Oce
Oce el 12 de Sept. de 2018
File answer, it's working for me:
Files = dir('Events/*_OutputFile.txt'); %List folder content
Nbr_Event = length(Files);
min_picks = 8;
for i = 1:Nbr_Event;
filename = sprintf ('Events/%d_OutputFile.txt',i);
Events = fileread(filename);
occ_pha = count(Events,[" EP "," ES "]);
if occ_pha >= min_picks;
fid = fopen('test.txt','a');
fprintf(fid, Events, '\n');
fprintf(fid, '\n');
fclose(fid)
end
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Standard File Formats en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by