Error with using xlsread on 9000 files

5 visualizaciones (últimos 30 días)
Roman
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
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
Roman
Roman el 1 de Feb. de 2016
To be honest, I quit using textscan becaus for my problem I found xlsread to be much easier to use. I always have a column of dates and two columns of numeric data, which can be easily seperated using xlsread.
And leaving out the one error I have everytime I run the program, I am very happy with xlsread.
Nevertheless thank you for your answer Ingrid.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
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.
  2 comentarios
Roman
Roman el 2 de Feb. de 2016
Thank you very much Stephen!
Since I have read everything with xlsread until now, everything was in cells anyways, so it's no problem dealing with that. I will try your solution on the whole 9000 files and tell you if I can successfully read all of them.
Roman
Roman el 2 de Feb. de 2016
Works perfectly fine, thank you! And it's way faster, too.

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
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
Roman
Roman el 2 de Feb. de 2016
@Stephan:
Here is a samplefile of how my data looks like:
Stephen23
Stephen23 el 2 de Feb. de 2016
Thank you. I wrote an Answer using your CSV file.

Iniciar sesión para comentar.


Image Analyst
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
Roman
Roman el 1 de Feb. de 2016
Well it takes about 20 minutes which I consider to be quite okay, if it would work without my interference.
Image Analyst
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Text Data Preparation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by