I/O operations How to delete a file if exist?

125 ビュー (過去 30 日間)
Amit Kumar
Amit Kumar 2013 年 2 月 22 日
コメント済み: Dr.Rajesh T.M 2019 年 5 月 28 日
Hi, I have written a function in Matlab and want to write results in a text file, which will be stored in working directory. But, I want to delete the result file if already exist and write new results. How to achieve this? Any help most welcome. Thanks.

採用された回答

TAB
TAB 2013 年 2 月 22 日
編集済み: TAB 2013 年 2 月 22 日
You can open the file directly in write mode
fh = fopen('ResultFile.txt','w');
If file already exists, this will over-write the all old contents of text file and you can write simply new contents. If the file not exist it will create a empty file. No need to manually check and delete file.
Still if you need check & delete option
if exist('ResultFile.txt', 'file')==2
delete('ResultFile.txt');
end
  1 件のコメント
Dr.Rajesh T.M
Dr.Rajesh T.M 2019 年 5 月 28 日
IT works really fine. Thank u

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by