Count occurrence in a file with text and number

조회 수: 2 (최근 30일)
Oce
Oce 2018년 9월 11일
댓글: Oce 2018년 9월 12일
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
Stephen23
Stephen23 2018년 9월 12일
@Oce: please do not close question that have answers.
Oce
Oce 2018년 9월 12일
Ok :-)

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2018년 9월 11일
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
Image Analyst
Image Analyst 2018년 9월 12일
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 2018년 9월 12일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by