How to use readtable when skipping every second (non-data) line?
조회 수: 28 (최근 30일)
이전 댓글 표시
Hi all,
I'm trying to load a file using readtable - see attached snip. The file has 314 colums and many rows. Every second row consists of actual data (e.g. line 10, 12, 14...) and the other rows are headerlines (lines <8) or non-data (e.g. line 9, 11, 13...). How can I read this file so it skips all the non-data lines?
I know how to skip headerlines: data = readtable ('file.dat', 'NumHeaderLines', 10);
But I don't know how to skip every second line. I've been trying to play around with detectImportOptions en setvaropts, but so far (very) unsuccesful. Help! :)
댓글 수: 6
dpb
2022년 5월 9일
This is an abomination -- how was this file created; no chance of fixing it on creation I suppose?
채택된 답변
추가 답변 (2개)
Mathieu NOE
2022년 5월 9일
hello
try this
T = readtable ('samplefile.txt', 'NumHeaderLines', 8,"Delimiter",' ');
[m,n] = size(T);
select_rows = 1:2:m;
TT = T(select_rows,:)
댓글 수: 2
Mathieu NOE
2022년 5월 10일
hello
can you share the entire file and tell us which data is to be retrieved (important) and what can be left behind ?
dpb
2022년 5월 9일
>> C=readcell('samplefile.txt');
>> C=C(9:2:end,:);
>> C(1:12,:)
ans =
12×3 cell array
{[16]} {[26]} {'52.7 1900 200 450→→ RGC→154→0…'}
{[16]} {[26]} {'52.7 1900 200 450→→ RGC→154→0…'}
{[16]} {[26]} {'52.9 1902 200 450→→ RGC→154→0…'}
{[16]} {[26]} {'53.0 1904 200 450→→ RGC→154→0…'}
{[16]} {[26]} {'53.1 1906 200 450→→ RGC→154→0…'}
{[16]} {[26]} {'53.2 1908 200 450→→ RGC→154→0…'}
{[16]} {[26]} {'53.3 1910 200 450→→ RGC→154→0…'}
{[16]} {[26]} {'53.4 1912 200 450→→ RGC→154→0…'}
{[16]} {[26]} {'53.6 1914 200 450→→ RGC→154→0…'}
{[16]} {[26]} {'53.7 1916 200 450→→ RGC→154→0…'}
{[16]} {[26]} {'53.8 1918 200 450→→ RGC→154→0…'}
{[16]} {[26]} {'53.9 1920 200 450→→ RGC→154→0…'}
>> strlength(C(1,3))
ans =
2199
>> sum(C{1,3}==9)
ans =
618
>> D=split(C(1,3));
>> whos D
Name Size Bytes Class Attributes
D 314x1 35256 cell
>> D=str2double(split(C(1,3)));
>> whos D
Name Size Bytes Class Attributes
D 314x1 2512 double
>> D(1:20)
ans =
52.7
1900
200
450
NaN
154
0
0
0
0
0
0
0
0
464.2
459.9
456.5
453.7
450.7
447.8
>> sum(isfinite(D))
ans =
313
>> C{1,3}(1:50)
ans =
'52.7 1900 200 450 RGC 154 0.0'
>>
Do you know how many variables are in each file a priori, somehow?
The multiple tabs really screw things up although it looks like maybe can deal with it.
I've not had much direct success with in import options object although with enough time/effort it can undoubtedly be done.
I might just resort to the old "one at a time" here with fgetl and split(), though, and be done with it.
참고 항목
카테고리
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!