How to write a Table into an Excel File?
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!