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
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
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
2018년 9월 3일
No it seems to be marked as r2018a
Tibo
2018년 9월 4일
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!