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

213 visualizaciones (últimos 30 días)
Amit Kumar
Amit Kumar el 22 de Feb. de 2013
Comentada: Dr.Rajesh T.M el 28 de Mayo de 2019
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.

Respuesta aceptada

TAB
TAB el 22 de Feb. de 2013
Editada: TAB el 22 de Feb. de 2013
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

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by