MATLAB: Error with using xlsread on 9000 files

trycatchxlsread

Hello dear friends,
I have a problem with using xlsread on a large amount of files (over 9000 excel files). When I try to read every single one of them, I get a random error after about 5000 files. When I manually continue with the exact same file that causes the error, it will work just fine. But I am working on automatically reading all the files without having to manually correct each time I try to load them into my workspace.
So what I tried to do is that I used try and catch, something like this (pseudo-code):
i=1;
while i<MaxNumberOfFiles
try
data(i) = xlsread(...);
i=i+1;
catch
end
end
But it won't work since it's caught in the loop without end (always catches the same error). I think it might have something to do with restarting the whole "Matlab-running-the-process" when I manually correct the error and it works. Did anyone experience something like that before and could I avoid the error by using something like a timeout or memory-refreshing?
Looking forward to your ideas
Roman

Best Answer

Here is one way of reading semi-colon delimited CSV [sic] files quickly using textscan, which has the advantage that the file can be closed via fclose. The data file you supplied (attached below) is complicated by the use of a comma , as the decimal radix point, so I read the second column as strings to convert to numeric later. As such it would be easier if the file was saved as a true CSV file (with , delimiter and . radix point).
In any case, this will read your sample data file:
% Read CSV file data:
fid = fopen('SampleFile.csv','rt');
hdr = regexp(fgetl(fid),';','split');
C = textscan(fid,'%s%s%f','Delimiter',';');
fclose(fid);
% Convert decimal comma to period:
C{2} = str2double(strrep(C{2},',','.'));
% Convert to numeric matrix and serial date number:
mat = horzcat(C{2:3});
dtn = cellfun(@datenum8601,C{1});
Note that the last line requires my FEX submission datenum8601, which will convert those beautiful ISO 8601 date strings into serial date numbers.
Related Question