Error with using xlsread on 9000 files
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Roman
el 1 de Feb. de 2016
Comentada: Roman
el 2 de Feb. de 2016
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
2 comentarios
Ingrid
el 1 de Feb. de 2016
have you tried if the problem also occurs if you use textscan? I always use this one because you can specifically close the file again (fopen and fclose) and you have more possibilities of manipulating how the data is read in
Respuesta aceptada
Stephen23
el 2 de Feb. de 2016
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.
Más respuestas (2)
Walter Roberson
el 1 de Feb. de 2016
Some MATLAB versions have failed to close xls files after reading them, which causes the session to run out of open file descriptors. If that is happening to you then a periodic close('all') can be useful (but of course that will close any file you might have wanted left open, such as if you were writing results to a file, so you need to take that into account.)
15 comentarios
Image Analyst
el 1 de Feb. de 2016
Processing 9000 files with xlsread() will basically take .... well .... forever. This is because it must launch Excel, do the reading, and then shut down Excel. You know how long it takes to launch Excel? Well imagine doing that 9000 times. There is no way you should be doing that with R2015a. I use Active X for anything over about 3 files. You must use ActiveX if you want to get done this year. I attach a demo.
2 comentarios
Image Analyst
el 1 de Feb. de 2016
You might try readtable() instead of xlsread(). It generates a table instead of a cell array, which has way less memory so it might be faster than xlsread(). Worth trying anyway. Time with tic and toc.
Ver también
Categorías
Más información sobre Text Data Preparation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!