Clear Filters
Clear Filters

How to export a set of tables to Excel

40 views (last 30 days)
Mahnoor
Mahnoor on 7 Aug 2023
Edited: Mahnoor on 9 Aug 2023
Hi, would anyone please be able to assist with this.
I have written several lines of code that generate specific tables. I would like to export all of these tables (2 examples shown below) into one Excel spreadsheet.
For example these are the lines of code with their generated tables all in one script. How can I use the 'writetable' function for both of these tables?

Answers (2)

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 7 Aug 2023
% Create a cell array to hold your tables
tables = {table1, table2, table3}; % Replace with your actual tables
% Specify the filename for the Excel file
excelFilename = 'output.xlsx'; % Replace with your desired filename
% Write each table to a separate sheet in the Excel file
for i = 1:length(tables)
sheetName = sprintf('Sheet%d', i); % Change to your desired sheet names
writetable(tables{i}, excelFilename, 'Sheet', sheetName);
end
  1 Comment
Mahnoor
Mahnoor on 8 Aug 2023
Edited: Mahnoor on 8 Aug 2023
Hello @Mrutyunjaya Hiremath, thankyou for your support.
I have tried the following code but does not seem to be working. I get an error saying 'Invalid parameter name: Total Driveshaft Torque' in the workspace box.
% Create a cell array to hold your tables
tables = {Total_Drivehsaft_Torque, Driveshaft_Speed }; % Replace with your actual tables
% Specify the filename for the Excel file
excelFilename = 'Excel Output for SRDC_MIDAS_Simulations.xlsx'; % Replace with your desired filename
% Write each table to a separate sheet in the Excel file
for i = 1:length(tables)
sheetName = sprintf('Total Driveshaft Torque', 'Driveshaft Speed', i); % Change to your desired sheet names
writetable(tables{i}, excelFilename, 'Total Driveshaft Torque', 'Driveshaft Speed', sheetName);
end
I beleive I have not formatted the 'for loop' properly and not sure how to write the names of the Sheets properly which will be Total Driveshaft Torque and Driveshaft Speed.
Thankyou.

Sign in to comment.


Star Strider
Star Strider on 7 Aug 2023
One option using writetable is to use the 'Sheet' name-value pair (the first entry in the documentation section on Spreadsheet Files Only). Use a for loop and wither the index number for the sheet designator or a separate string or cell array containing the desired sheet names if you want to use those instead, and index into them.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!