How to write a Table into an Excel File?

조회 수: 2 (최근 30일)
ercan duzgun
ercan duzgun 2021년 3월 25일
댓글: Walter Roberson 2021년 3월 26일
Dear Matlab users;
I am trying to write a table with its captions to an Excel file. I have checked the Matlab website here : https://www.mathworks.com/help/matlab/import_export/exporting-to-excel-spreadsheets.html . However I am having problems with it. Could you help me?
My code is below:
clc
AAA=fprintf(' L1 L2 L3 L4 L5 L6 G_phi12 G_phi34 G_phi56 H_phi12 H_phi34 H_phi56 P1x P1y P1z P2x P2y P2z P3x P3y P3z \n');
BBB=fprintf('---------------------------------------------------------------------------------------------------------------------------------\n');
Table3=[1.333 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21;
1.555555 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21;
1.66666 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21];
KKK=round(Table3,2);
filename = 'testdata2.xlsx';
writematrix(AAA,filename,'Sheet',1,'Range','A1:U1')
writematrix(BBB,filename,'Sheet',1,'Range','A2:U2')
writematrix(KKK,filename,'Sheet',1) % It is not certain the size of the Table3. The raw number of Table3 might be larger than 3.

답변 (1개)

Walter Roberson
Walter Roberson 2021년 3월 25일
AAA = sprintf(' L1 L2 L3 L4 L5 L6 G_phi12 G_phi34 G_phi56 H_phi12 H_phi34 H_phi56 P1x P1y P1z P2x P2y P2z P3x P3y P3z \n');
BBB = sprintf('---------------------------------------------------------------------------------------------------------------------------------\n');
Notice sprintf not fprintf for this purpose.
  댓글 수: 3
ercan duzgun
ercan duzgun 2021년 3월 25일
편집: ercan duzgun 2021년 3월 25일
Dear @Walter Roberson thank you very much for your answer. How can I avoid of overwriting?
And how can I write elements of AAA into each cell of Excel separetely?
Walter Roberson
Walter Roberson 2021년 3월 26일
filename = 'testdata2.xlsx';
AAA = {'L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'G_phi12', 'G_phi34', 'G_phi56', 'H_phi12', 'H_phi34', 'H_phi56', 'P1x', 'P1y', 'P1z', 'P2x', 'P2y', 'P2z', 'P3x', 'P3y', 'P3z'};
Table3=[1.333 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21;
1.555555 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21;
1.66666 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21];
T = array2table(Table3, 'VariableNames', AAA);
writetable(T, filename);
What this will not do is write the line of dashes between the variable names and the contents. If the dashes are needed for some reason, then it would probably be easiest to use writecell()
filename = 'testdata2.xlsx';
dashes_per_variable = 3;
AAA = {'L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'G_phi12', 'G_phi34', 'G_phi56', 'H_phi12', 'H_phi34', 'H_phi56', 'P1x', 'P1y', 'P1z', 'P2x', 'P2y', 'P2z', 'P3x', 'P3y', 'P3z'};
Table3=[1.333 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21;
1.555555 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21;
1.66666 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21];
dash = repmat('-', 1, dashes_per_variable);
BBB = repmat({dash}, 1, size(AAA,2));
CCC = num2cell(Table3);
DDD = [AAA;BBB;CCC];
writecell(DDD, filename);

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

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by