MATLAB Answers

Thar
0

Ignore text in a txt file

Asked by Thar
on 18 Feb 2019
Latest activity Commented on by Star Strider
on 18 Feb 2019
Hi all!
I have 50 txt files name 001.txt until 050.txt and look like:
time [ms]: 100,00
Nr. [scans]: 1
Nr. [pixels]: 0
Data [m]
[first] [second] [third] [fourth]
296,95; 511,000; 11,000; 0,000
297,04; 503,000; 15,333; 0,000
297,13; 505,000; 15,000; 0,000
297,22; 553,000; 16,667; 0,000
297,31; 515,000; 2,333; 0,000
297,40; 541,000; -3,333; 0,000
297,49; 505,000; -1,333; 0,000
297,58; 530,000; 7,333; 0,000
297,67; 506,000; 9,000; 0,000
I want to ignore the line with text and keep only the numeric columns of all 50 files.
Any ideas??
Thank you!

  0 Comments

Sign in to comment.

1 Answer

Answer by Star Strider
on 18 Feb 2019

It depends on the function you are using to read them. In the textscan (link) and other functions that use textscan, they are called header lines. You can skip over them by using the 'HeaderLines' (link) name-value pair. Other functions handle them differently, so for example in dlmread (link) you would specify the number of rows to skip using the Starting Row Offset (link) number.
Choose a function and read the documentation for it!

  3 Comments

I used this:
folder = 'F:\fi\di';
Allfiles = dir(fullfile(folder, '*.txt'));
for k = 1:length(Allfiles)
initialFileName = Allfiles(k).name;
fullFileName = fullfile(folder, initialFileName);
c(k)=textscan( fullFileName, '%f%f%f%f','headerlines','5' );
end
but something is wrong. The message is:
Error using textscan
HeaderLines must be integer-valued.
'HeaderLines', 5
@Walter — Thank you yet again!

Sign in to comment.