read certain rows and lines into array from txt file

2 次查看(过去 30 天)
i need to read the numbers in the columns and rows from a text file with a number of header lines and end lines into an array
some header lines (10 odd)
S,Sweep No, N,Test No, L,Length, A,Act Length, S,Start Time, E,End Time, T1,Transition 1, T2,Transition 2, O,Outcome
S,0, N,0, L,156998, A,156998, S,7672055, E,7829053, T1,7672056, T2,7672056, O,0
S,0, N,1, L,156998, A,156998, S,7972052, E,8129050, T1,7974053, T2,7974053, O,0
S,0, N,2, L,156998, A,156998, S,8272048, E,8429046, T1,8276049, T2,8276049, O,0
S,0, N,3, L,156998, A,156998, S,8572044, E,8729042, T1,8578045, T2,8578045, O,0
S,0, N,4, L,156998, A,156998, S,8872041, E,9029039, T1,8880042, T2,8880042, O,0
S,0, N,5, L,156998, A,156998, S,9172037, E,9329035, T1,9182038, T2,9182038, O,0
S,0, N,6, L,156998, A,156998, S,9472034, E,9629032, T1,9484035, T2,9484035, O,0
S,0, N,7, L,156998, A,156998, S,9772030, E,9929028, T1,9786031, T2,9786031, O,0
End of Test
some end lines (a couple)
example of array:
0 0 99998 99999 8361007 8461006 8361008 8361008 0
0 1 99998 99998 8561005 8661003 8571006 8571006 0
0 2 99998 99999 8761002 8861001 8781003 8781003 0
0 3 99998 99998 8961000 9060998 8991001 8991001 0
0 4 99998 99999 9160997 9260996 9200998 9200998 1
0 6 99998 99999 9560992 9660991 9620993 9620993 0
0 7 99998 99998 9760990 9860988 9830991 9830991 0
the number of rows will be different depending on the text file but the format of the data i need will be the same
  3 个评论
smalltony
smalltony 2019-10-23
How would I get rid of the bottom text that i don't need
dpb
dpb 2019-10-23
编辑:dpb 2019-10-23
Tell the import object to not fill in the rows w/ missing data.
Or, if don't do that, will return rows consisting all of NaN values that can be deleted.

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2019-10-23
opt=detectImportOptions('smtony.txt'); % get the base import object
opt.SelectedVariableNames=opt.SelectedVariableNames(2:2:end); % tell it only want numeric columns (every other one)
opt.MissingRule='omitrow'; % don't fill missing variables, skip instead
The above on the sample file returns:
>> t=readtable('smtony.txt',opt)
t =
8×9 table
SweepNo TestNo Length ActLength StartTime EndTime Transition1 Transition2 Outcome
_______ ______ ________ _________ __________ __________ ___________ ___________ _______
0 0 1.57e+05 1.57e+05 7.6721e+06 7.8291e+06 7.6721e+06 7.6721e+06 0
0 1 1.57e+05 1.57e+05 7.9721e+06 8.1291e+06 7.9741e+06 7.9741e+06 0
0 2 1.57e+05 1.57e+05 8.272e+06 8.429e+06 8.276e+06 8.276e+06 0
0 3 1.57e+05 1.57e+05 8.572e+06 8.729e+06 8.578e+06 8.578e+06 0
0 4 1.57e+05 1.57e+05 8.872e+06 9.029e+06 8.88e+06 8.88e+06 0
0 5 1.57e+05 1.57e+05 9.172e+06 9.329e+06 9.182e+06 9.182e+06 0
0 6 1.57e+05 1.57e+05 9.472e+06 9.629e+06 9.484e+06 9.484e+06 0
0 7 1.57e+05 1.57e+05 9.772e+06 9.929e+06 9.786e+06 9.786e+06 0
>>

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by