reading text fils with data importing

1 次查看(过去 30 天)
I have a long text file with data. There are "epochs" starting with EP and "information" i few next lines like that:
EP 00 00 00
G03
F17
D32
EP 00 00 30
G01
F04
D31
G03
H34
EP 00 01 00
B34
K05
L22
H34
H11
G11
EP 00 01 30
H90
G03
EP...
I need to read all epochs and if in a given epoch there will be a line with information of my choice, containing, for example "G03" I need confirmation this e.g. as "1" and if these information is not at epoch confirmation as e.g. "0". The are two problems from my point of view: there could be different number of lines with "information" in particular epochs and "information" of my choice may lie in any line.
Thank you in advance for any suggestions

采纳的回答

Stephen23
Stephen23 2025-2-26
编辑:Stephen23 2025-2-26
It would be much better if you uploaded a sample data file by clicking the paperclip button.
In lieu of that I created my own demo data file:
str = fileread('myfile.txt')
str =
'EP 00 00 00 G03 F17 D32 EP 00 00 30 G01 F04 D31 G03 H34 EP 00 01 00 B34 K05 L22 H34 H11 G11 EP 00 01 30 H90 G03'
tkn = regexp(str,'EP(\s+\d+){3}(\s+[A-Z]\d+)+','tokens');
tkn = vertcat(tkn{:});
spl = regexp(tkn(:,2),'\w+','match');
uni = unique([spl{:}]);
drn = duration(sscanf([tkn{:,1}],'%u',[3,Inf]).')
drn = 4x1 duration array
00:00:00 00:00:30 00:01:00 00:01:30
fnh = @(t)contains(uni,t);
tmp = cellfun(fnh,spl,'uni',0);
tmp = vertcat(tmp{:});
tbl = array2timetable(tmp, 'RowTimes',drn, 'VariableNames',uni)
tbl = 4x13 timetable
Time B34 D31 D32 F04 F17 G01 G03 G11 H11 H34 H90 K05 L22 ________ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ 00:00:00 false false true false true false true false false false false false false 00:00:30 false true false true false true true false false true false false false 00:01:00 true false false false false false false true true true false true true 00:01:30 false false false false false false true false false false true false false

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by