Why xlswrite slow down in a for loop?

Hello,
when I use xlswrite in a for loop, the time spend by xlswrite increases at each loop. The following code is an illustration of the problem. Thanks in advance if somebody knows a solution to fix the problem
if true
clear all
close all
Nb=500;
Time=zeros(Nb,1)+nan;
filename = 'testdata.xlsx';
sheet = 'MySheet';
xlRange = 'E1';
t1=tic;
for i=1:Nb
t0=tic;
A = {'Time','Temperature'; rand,rand; rand,rand; rand,rand};
xlswrite(filename,A,sheet,xlRange)
Time(i)=toc(t0);
end
toc(t1)
figure
plot(Time(2:end))
grid on
end

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 9 月 3 日

0 投票

Xlsx files are zipped structures of text files. Each time you xlswrite it has to parse the existing text, make the appropriate binary adjustments, and write it all out and zip again.
Writing incrementally to spreadsheets is not recommended.
You must be using Windows or else you would have received an error message when you tried to write nonnumeric values with xlswrite. Consider instead using ActiveX to talk to excel to make the updates. Or better yet, accumulate all of the data and write it at the end.

3 件のコメント

Image Analyst
Image Analyst 2018 年 9 月 3 日
I'm kind of surprised this slows it down because I think that starting a few versions ago they switched from launching Excel every time (which would be very slow) to using ActiveX and leaving the server running, which is much, much faster. I think Tibo must be using a version several years old.
Walter Roberson
Walter Roberson 2018 年 9 月 3 日
No it seems to be marked as r2018a
Tibo
Tibo 2018 年 9 月 4 日
Hello, matlab version r2018a
excel version 2016
windows 7
At the begining I thought that it was a version compatibility problem. The problem exists with different versions of matlab and excel excel

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

製品

リリース

R2018a

タグ

質問済み:

2018 年 9 月 3 日

コメント済み:

2018 年 9 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by