reading text fils with data importing
조회 수: 2 (최근 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
댓글 수: 0
채택된 답변
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')
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]).')
fnh = @(t)contains(uni,t);
tmp = cellfun(fnh,spl,'uni',0);
tmp = vertcat(tmp{:});
tbl = array2timetable(tmp, 'RowTimes',drn, 'VariableNames',uni)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!