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
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!