How do I get readtable to skip lines with certain text?

50 Ansichten (letzte 30 Tage)
Erik Hall
Erik Hall am 26 Jul. 2021
Beantwortet: dpb am 26 Jul. 2021
Text file for example:
2021/07/20 17:32:11> VO1= 81 VS1= 80 I1= 10.2 VO2= 0 VS2= 0 I2= 0.0
2021/07/20 17:32:11> HV ramping finished.
2021/07/20 17:33:04> VO1= 100 VS1= 100 I1= 5.0 VO2= 0 VS2= 0 I2= 0.0
How do I get readtable to skip lines with text like the line with "HV ramping finished"? I've tried using TreatAsMissing and CommentStyle but neither seemed to work.

Akzeptierte Antwort

dpb
dpb am 26 Jul. 2021
That's not a great format for readtable to try to parse -- it's got the variable names as columms and so are interpreted as data -- and the 'TreatAsMissing' works only for numeric variables for some reason I've never fully understood/yet understand the logic behind.
It takes quite a lot of munging on the import options object, but one can make it happen -- whether it's easier that way or to just read as cellstr array and edit in place is probably a tossup..
opt=detectImportOptions('erik.dat','Delimiter',{'>','='},'ReadVariableNames',0); % start with basic object
opt.DataLines=[1 inf]; % it wants skip the second record on its own
opt.MissingRule='omitrow'; % this fixes up the bum row
opt.VariableTypes=[{'datetime','char'} repmat({'double'},1,6)]; % set the types since it can't tell with the Vxx=
opt=setvaropts(opt,3:8,'trimnonnumeric',1); % this strips the variable name
opt.SelectedVariableNames=opt.VariableNames([1 3:end]); % the first V01 is second variable
opt=setvaropts(opt,"Var1","InputFormat",'yyyy/MM/dd HH:mm:ss'); % import datetime format
tErik=readtable('erik.dat',opt,'ReadVariableNames',0);
This returns
>> tErik
tErik =
2×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7
___________________ ______ ______ _____ ____ ____ ____
2021/07/20 17:32:11 81.00 80.00 10.20 0.00 0.00 0.00
2021/07/20 17:33:04 100.00 100.00 5.00 0.00 0.00 0.00
>>
This doesn't return the variable names; that can be managed by reading one record first and parsing it for the variable names that can be used to set them programmatically.

Weitere Antworten (0)

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by