What is the fastest way to export a large matrix of numbers from MATLAB to Excel (preferably as .xlsx) ?

조회 수: 20 (최근 30일)
What is the fastest way to export a large matrix of numbers from MATLAB to Excel (preferably as .xlsx) ?
It looks like there are several ways, such as writematrix, writecell, writetable, xlswrite, csvwrite (apparently, xlswrite and csvwrite are not recommended), etc, but which one is the fastest one for large matrices ?
% Example of matrix of numbers to be exported from Matlab to Excel (preferably as .xlsx):
M = magic(10^3);

채택된 답변

Jan
Jan 2022년 11월 19일
편집: Jan 2022년 11월 19일
Simply try it:
M = magic(10^3);
tic;
writematrix(M, 'file1.xlsx');
toc
Elapsed time is 5.094270 seconds.
tic;
writecell(num2cell(M), 'file2.xlsx');
toc
Elapsed time is 10.870852 seconds.
tic;
writetable(table(M), 'file3.xlsx');
toc
Elapsed time is 4.806810 seconds.
tic;
xlswrite('file4.xlsx', M);
Warning: Unable to write to Excel format, attempting to write file to CSV format.
toc
Elapsed time is 0.736247 seconds.
tic;
csvwrite('file5.csv', M);
toc
Elapsed time is 0.481182 seconds.
tic;
fid = fopen('file6.csv', 'W');
fmt = [repmat('%g, ', 1, 999), '%g\n'];
fprintf(fid, fmt, M.');
fclose(fid);
toc
Elapsed time is 0.402371 seconds.
tic;
fid = fopen('file7.csv', 'W');
fmt = [repmat('%g, ', 1, 999), '%g\n'];
fwrite(fid, sprintf(fmt, M.'), 'char');
fclose(fid);
toc
Elapsed time is 0.424347 seconds.
  댓글 수: 1
Sim
Sim 2022년 11월 19일
It is interesting that, although xlswrite and csvwrite are not recommended, they still outperform the write** functions.
Also, it is interesting that fprintf and fwrite are the fastest solutions available in your tests :-)
Many thanks @Jan!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by