How to use readtable when skipping every second (non-data) line?
29 ビュー (過去 30 日間)
古いコメントを表示
Sjoukje de Lange
2022 年 5 月 9 日
編集済み: Sjoukje de Lange
2022 年 5 月 10 日
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!