How to ignore letters while using fscanf?

I have data sets that are text file that look something like this:
Area Mean Min Max
1 25 239 170 351
2 25 250.120 155 442
3 25 259.360 159 477
I have been using a code that uses fscanf to read these files into arrays. However, I have to delete the header of the text files for it to work properly. Here is the bit of code with fscanf:
fprintf('\n reading in .txt file')
if nFile > 1
filename = filenameAll{dataind};
formatSpec = '%f%f%f%f%f%[^\n\r]';
fid = fopen([pathname filename]);
rawdata=fscanf(fid, '%f %f %f %f %f', [6 inf]);
rawdata=rawdata';
else
fid = fopen([pathname filenameAll]);
rawdata=fscanf(fid, '%f %f %f %f %f', [6 inf]);
rawdata=rawdata';
end
Is there a way to "tell" the code to ignore alphabetical letters? Or instead, to remove the letters before using fscanf?
Thanks in advance!

2 Comments

formatSpec is not used. Does the file really contain blank lines after each line of data?
No, there aren't blank lines after the lines of data, the lines just showed up when I copy-pasted some data.

Sign in to comment.

 Accepted Answer

fid = fopen(fullfile(pathname, filenameAll));
fgets(fid); % Ignore first line
rawdata = fscanf(fid, '%f %f %f %f %f', [6 inf]);

3 Comments

This works. Thanks for your help!
Hi Jan,
Is there any faster way of ignoring the first line? I need precise timing and it is taking ~1.5secs to run
@Killian: Reading one line cannot take 1.5 seconds, except it has many MB of data. What exactly takes 1.5 seconds? How large is the file? Is it stored on a network drive?

Sign in to comment.

More Answers (0)

Tags

Asked:

on 8 Oct 2013

Commented:

Jan
on 3 Mar 2017

Community Treasure Hunt

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

Start Hunting!