Error with using xlsread on 9000 files

7 ビュー (過去 30 日間)
Roman
Roman 2016 年 2 月 1 日
コメント済み: Roman 2016 年 2 月 2 日
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 件のコメント
Ingrid
Ingrid 2016 年 2 月 1 日
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 2016 年 2 月 1 日
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.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2016 年 2 月 2 日
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 件のコメント
Roman
Roman 2016 年 2 月 2 日
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 2016 年 2 月 2 日
Works perfectly fine, thank you! And it's way faster, too.

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 2 月 1 日
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 件のコメント
Roman
Roman 2016 年 2 月 2 日
@Stephan:
Here is a samplefile of how my data looks like:
Stephen23
Stephen23 2016 年 2 月 2 日
Thank you. I wrote an Answer using your CSV file.

サインインしてコメントする。


Image Analyst
Image Analyst 2016 年 2 月 1 日
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 件のコメント
Roman
Roman 2016 年 2 月 1 日
Well it takes about 20 minutes which I consider to be quite okay, if it would work without my interference.
Image Analyst
Image Analyst 2016 年 2 月 1 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by