How to ignore letters while using fscanf?

20 views (last 30 days)
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
Jan
Jan on 8 Oct 2013
formatSpec is not used. Does the file really contain blank lines after each line of data?
Katherine
Katherine on 8 Oct 2013
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

Jan
Jan on 8 Oct 2013
fid = fopen(fullfile(pathname, filenameAll));
fgets(fid); % Ignore first line
rawdata = fscanf(fid, '%f %f %f %f %f', [6 inf]);
  3 Comments
Killian McManus
Killian McManus on 2 Mar 2017
Hi Jan,
Is there any faster way of ignoring the first line? I need precise timing and it is taking ~1.5secs to run
Jan
Jan on 3 Mar 2017
@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

Community Treasure Hunt

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

Start Hunting!