create exceltable with multiple sheets in matlab

Here's an example code that creates a cell array of tables with multiple sheets in an Excel file:

main.m
% Create first table as a MATLAB table
t1 = table(rand(5,3),rand(5,3),rand(5,3),'VariableNames',{'A','B','C'});

% Create second table as a MATLAB table
t2 = table(rand(5,3),rand(5,3),rand(5,3),'VariableNames',{'X','Y','Z'});

% Store both tables in a cell array
t = {t1, t2};

% Define Excel file name
filename = 'multisheet.xlsx';

% Write cell array of tables to an Excel file with multiple sheets
writetable(t{1}, filename, 'Sheet', 'Sheet1');
for ii=2:numel(t)
    writetable(t{ii}, filename, 'Sheet', ['Sheet',num2str(ii)]);
end
535 chars
18 lines

The above code creates two tables as MATLAB tables and stores them in a cell array. Then, writetable() function is used to write each table to a separate sheet in an Excel file. The loops are used to write each table to the Excel file. You can adjust the loop based on how many tables you want to store in the cell array.

related categories

gistlibby LogSnag