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

1 回表示 (過去 30 日間)
Brad
Brad 2013 年 6 月 24 日
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 Center および File ExchangeLanguage Support についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by