How to use readtable when skipping every second (non-data) line?

27 views (last 30 days)
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 Comments
dpb
dpb on 9 May 2022
This is an abomination -- how was this file created; no chance of fixing it on creation I suppose?
Sjoukje de Lange
Sjoukje de Lange on 10 May 2022
Yeahh... the way this file is saved is absolutely painful. But no way of fixing it indeed, it's straight output from an measurement instrument.

Sign in to comment.

Accepted Answer

Sjoukje de Lange
Sjoukje de Lange on 10 May 2022
Edited: Sjoukje de Lange on 10 May 2022
I think I fixed it (for future reference)! It might be not as clean and professional as it can be, but I think this works:
%read data
fid = fopen(file);
formatspec = ['%D%f%f%f%s', repmat('%f', 1, 309)];
c = textscan(fid, formatspec, 'headerLines', 10);
fclose (fid);
clear line;
line=table; %make empty table
%fill table
for i = 1:length(c)
line{:,i} = (c{i});
end
%delete every second row
line(1:2:end,:) = [];
clear formatspec c header t fid i

More Answers (2)

Mathieu NOE
Mathieu NOE on 9 May 2022
hello
try this
T = readtable ('samplefile.txt', 'NumHeaderLines', 8,"Delimiter",' ');
[m,n] = size(T);
select_rows = 1:2:m;
TT = T(select_rows,:)
  2 Comments
Sjoukje de Lange
Sjoukje de Lange on 10 May 2022
Thanks for your suggestion! The file has 314 columns and many rows. The problem is that readtable does not read the file as having 314 rows, but only 4, due to the rows without data. The data in the other lines is therefore merged in one column, without any clear delimiters.
Mathieu NOE
Mathieu NOE on 10 May 2022
hello
can you share the entire file and tell us which data is to be retrieved (important) and what can be left behind ?

Sign in to comment.


dpb
dpb on 9 May 2022
>> 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.
  1 Comment
Sjoukje de Lange
Sjoukje de Lange on 10 May 2022
Thanks for your suggestion! Playing around with fgetl and split seems to get me quite far!
By the way, the file is supposed to have 314 columns (the variables) and many (more than 10,000) lines (the measurement points).

Sign in to comment.

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!