writetable() to change header and append cell array in each row

14 ビュー (過去 30 日間)
chlor thanks
chlor thanks 2021 年 5 月 4 日
コメント済み: Scott MacKenzie 2021 年 5 月 4 日
I have a cell array made of cell array tables such that
testarray
= 1×2 cell array
{1×3 table} {1×3 table}
And
testarray{1}
= 1×3 table
Var1 Var2 Var3
___________________________________ ______ ______
'1' '2' '3'
testarray{2}
= 1×3 table
Var1 Var2 Var3
___________________________________ ______ ______
'3' '4' '5'
And desired header to be
col1 col2 col3
How do I use writetable() to write this in excel that looks like
col1 col2 col3
1 2 3
3 4 5
Thank you!!

採用された回答

chlor thanks
chlor thanks 2021 年 5 月 4 日
summary = vertcat(testarray{:});
summary.Properties.VariableNames = { col1', 'col2', 'col3'}
writetable(summary, 'summary.xlsx');

その他の回答 (1 件)

Scott MacKenzie
Scott MacKenzie 2021 年 5 月 4 日
t1 = table({'1'}, {'2'}, {'3'});
t2 = table({'4'}, {'5'}, {'6'});
t1.Properties.VariableNames = { 'col1', 'col2', 'col3' };
testarray = {t1, t2};
writetable(testarray{1}, 'test.txt');
writetable(testarray{2}, 'test.txt', 'writevariablenames', false, 'writemode', 'append');
In the command window...
>> type test.txt
col1,col2,col3
1,2,3
4,5,6
  2 件のコメント
chlor thanks
chlor thanks 2021 年 5 月 4 日
Thanks Scott! This will help me learn!
By the way, when I try this code it says 'writemode' is a unvalid parameter name, also t2 didnt append.
Scott MacKenzie
Scott MacKenzie 2021 年 5 月 4 日
Gee, that's weird. I just re-ran the code, no problem. writetable dates to R2013a, so that's unlikely an issue. Of course, t2 did not append becuase the 2nd writetable crashed. I really don't know what the issue. If somone else reads this and has an idea, perhaps they'll weight in.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by