Test EOF while reading a file using csvread function
Show older comments
I am currently reading a .csv file in matlab using csvread function, and I am specifying where to begin and end reading. I would like to know if I am able to test if the current ending position extends beyond the end of file. Thank you. Andrei
Answers (2)
Titus Edelhofer
on 2 May 2011
Hi Andrei,
not directly. There are several possibilities though: if you edit csvread, you see the call to dlmread. If you edit dlmread, you see somewhere around line 149 (depending on MATLAB version) a call to close the file:
fclose(fid);
So one option would be to copy both files to your local folder, rename them to e.g. mycsvread and mydlmread and before closing the file add something like
atTheEnd = feof(fid);
Or you could get the number of lines by
fid = fopen(filename, 'rt');
t = textscan(fid, '%s', 'delimiter', '\n');
nRows = length(t{1});
fclose(fid);
and compare nRows with you row parameter to csvread.
Titus
Andrei
on 2 May 2011
0 votes
Categories
Find more on Data Import from MATLAB 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!