How to write a Table into an Excel File?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Walter Roberson
il 25 Mar 2021
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 Commenti
Walter Roberson
il 26 Mar 2021
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);
Vedere anche
Categorie
Scopri di più su Spreadsheets in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!