Test EOF while reading a file using csvread function
显示 更早的评论
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
回答(2 个)
Titus Edelhofer
2011-5-2
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
类别
在 帮助中心 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!