Converting a cell array to excel file using write table - how to add headers and sheet name?

7 ビュー (過去 30 日間)
I am able to convert the 'THRESH_DATA' variable inside the attached to an Excel file - this is how my code does it (thanks, @Voss):
filename = 'THRESH_DATA.xlsx';
n = size(THRESH_DATA,1);
for ii = 1:n
T = table(THRESH_DATA{ii,:});
writetable(T,filename,'Sheet',ii);
end
The next step (which I was hoping to handle myself) is to include column headers and sheet names. For sheet names, I thought of:
% sheetNames = ('2000', '2001', '2002','2003', '2004', '2005','2006', '2007', '2008','2009', '2010', '2011',...
% '2012', '2013', '2014','2015', '2016', '2017','2018', '2019', '2020','2021', '2022');
writetable(T,filename,'Sheet',ii,'sheetNames')
but that doesn't work. Drawing from other posts (most using xlswrite), I thought I could used variableNames, somehow. Thanks!

採用された回答

Voss
Voss 2024 年 2 月 9 日
filename = 'THRESH_DATA.xlsx';
sheetNames = string(2000:2022);
% replace this with the variable names you want:
varNames = ["Time","X","Y","Z","AA","BBB","LMNOP","EIGHT","NOINE"];
n = size(THRESH_DATA,1);
for ii = 1:n
T = table(THRESH_DATA{ii,:},'VariableNames',varNames);
writetable(T,filename,'Sheet',sheetNames(ii));
end
  2 件のコメント
Paul Barrette
Paul Barrette 2024 年 2 月 9 日
This solution looks after both the sheet names and the headers. Thanks.
You're making this look so easy, @Voss.
Voss
Voss 2024 年 2 月 9 日
You're welcome! Glad to help

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

その他の回答 (1 件)

VBBV
VBBV 2024 年 2 月 9 日
編集済み: VBBV 2024 年 2 月 9 日
sheetNames = {'2000', '2001', '2002','2003', '2004', '2005','2006', '2007', '2008','2009', '2010', '2011',...
'2012', '2013', '2014','2015', '2016', '2017','2018', '2019', '2020','2021', '2022'};
writetable(T,filename,'Sheet',sheetNames{ii})
  2 件のコメント
VBBV
VBBV 2024 年 2 月 9 日
Declare the sheetNames as cell array and access the elements of cell array using the index of for loop
VBBV
VBBV 2024 年 2 月 9 日
編集済み: VBBV 2024 年 2 月 9 日
The '(' ')' are usually used as function definition calls, you could also use concatenation operator [ ] instead of curly braces { }

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

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by