How to overwrite my data everytime I run my code?

I'm currently writing a program and everytime I run it, it just adds the data to the excel vs deleting the contents of the excel and replacing it with new data. I'm using the writetable function which I thought would automatically clear the excel before filling it with new data. Any ideas?

 採用された回答

Hassaan
Hassaan 2024 年 8 月 13 日

0 投票

% Your data table
data = table(...); % Replace with your actual data
% Delete the existing file (if it exists)
if exist('your_file.xlsx', 'file')
delete('your_file.xlsx');
end
% Write the data to an Excel file and overwrite it
writetable(data, 'your_file.xlsx', 'WriteMode', 'overwrite');

3 件のコメント

Jørgen
Jørgen 2025 年 3 月 30 日
What if I export data to a specific Excel worksheet only, and I want to delete that sheet? I.e., I don't want to delete the workbook entirely.
Image Analyst
Image Analyst 2025 年 3 月 30 日
@Jørgen Are you using Windows? If so you can use ActiveX. See attached for a bunch of Excel utilities.
Walter Roberson
Walter Roberson 2025 年 3 月 30 日
When you call writetable(), specify 'WriteMode', 'overwritesheet'

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

その他の回答 (0 件)

製品

リリース

R2023b

質問済み:

KD
2024 年 8 月 13 日

コメント済み:

2025 年 3 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by