Can REGEXP be utilized to ignore specific numerical values in a text file?

조회 수: 1 (최근 30일)
I’ve got a text file that contains thousands of lines such as the following:
Code: 1 Firm ID: 5 Time Tag: 58000.00000000
Board ID: 1 SUB ID: 4 Error ID: 0 Abort: 0
Code: 6 Firm ID: 1 Time Tag: 58001.50000000
Board ID: 4 SUB ID: 5 Error ID: 1 Abort: 1
Code: 1 Firm ID: 1 Time Tag: 58002.00000000
Board ID: 4 SUB ID: 2 Error ID: 0 Abort: 1
Code: 3 Firm ID: 5 Time Tag: 58003.50000000
Board ID: 2 SUB ID: 2 Error ID: 0 Abort: 1
Code: 1 Firm ID: 4 Time Tag: 58004.00000000
Board ID: 9 SUB ID: 7 Error ID: 0 Abort: 0
Code: 1 Firm ID: 5 Time Tag: 58005.50000000
Board ID: 2 SUB ID: 1 Error ID: 0 Abort: 1
Code: 1 Firm ID: 5 Time Tag: 58006.00000000
Board ID: 7 SUB ID: 6 Error ID: 0 Abort: 0
Code: 9 Firm ID: 1 Time Tag: 58007.50000000
Board ID: 5 SUB ID: 3 Error ID: 0 Abort: 0
I’m attempting to parse out the time tag value and abort value for each SUB ID – except for SUB ID values 2 and 1.
I’m using the following code to parse out the time tags and abort values for ALL of the SUB IDs:
exp = '(?<=Time Tag: )([\d\.]+).+?(?<=SUB ID:[ ]+)(\d+).+?(?<=Abort:[ ]+)(\d+)';
tokens = regexp(buffer, exp, 'tokens');
OAM_data = reshape(str2double([tokens{:}]), 3, []).';
This results in the following 8 x 3 array:
58000 4 0
58001.5000000000 5 1
58002 2 1
58003.5000000000 2 1
58004 7 0
58005.5000000000 1 1
58006 6 0
58007.5000000000 3 0
If possible, how can I utilize the REGEXP command to parse out the time tags and abort values for all SUB IDs except for SUB IDs 1 and 2?

채택된 답변

Tom
Tom 2013년 6월 24일
TEXTSCAN is useful for extracting numeric data from strings. Note that the format string I used effectively reads two lines at once by searching for the end of line (\r\n) and ignoring it.
fid = fopen('Brad.txt');
fString = 'Code: %f Firm ID: %f Time Tag: %f %*[\r\n] Board ID: %f SUB ID: %f Error ID: %f Abort: %f';
S = textscan(fid,fString,'CollectOutput',true);
fclose(fid);
Data = S{1};
Data(Data(:,5) == 1 | Data(:,5) == 2 , :) = [];
timeTag = Data(:,3)
abort = Data(:,7)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by