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

Answers (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
I have managed to do a try-catch situation with a break condition in case of the eof (index out of bounds or something) error, and a while(true)

Categories

Tags

Asked:

on 2 May 2011

Community Treasure Hunt

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

Start Hunting!