フィルターのクリア

How to write a Table into an Excel File?

7 ビュー (過去 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 Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by