How to ignore letters while using fscanf?
20 views (last 30 days)
Show older comments
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
on 8 Oct 2013
formatSpec is not used. Does the file really contain blank lines after each line of data?
Accepted Answer
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
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
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?
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!