write table content into an exisiting excelfile in matlab

You can use MATLAB's built-in writetable function to write table content into an existing Excel file using the writetable function and specifying the sheet name. Here is an example:

main.m
% Load the existing Excel file
filename = 'existing_file.xlsx';
opts = spreadsheetImportOptions('Sheet', 'Sheet1');
existingData = readtable(filename, opts);

% Append new data to the existing table
newData = randn(5, 3);  % example data
newTable = array2table(newData, 'VariableNames', {'Var1', 'Var2', 'Var3'});
combinedTable = [existingData; newTable];

% Write the combined table into the original Excel file
writetable(combinedTable, filename, 'Sheet', 'Sheet1', 'WriteMode', 'overwrite');
495 chars
13 lines

This code first loads the existing data from an Excel file using readtable. It then generates some example data to be appended to the existing data, and creates a new table from it. The two tables are concatenated using square brackets to produce a new combined table. Finally, the combined table is written back into the original Excel file using writetable, targeting the sheet named "Sheet1" and overwriting any existing data in that sheet.

related categories

gistlibby LogSnag