MATLAB: Overwriting a diary file

command windowdiaryoutputsave

I want to save my output to .txt file. I have used
diary Output.txt
diary on
some code
diary off
It saves diary file in same directory. But when I run the same code again it writes the command window output under the previously saved content instead of overwriting. I want to overwrite the previous content and make a file having only single output.

Best Answer

dfile ='Output.txt';
if exist(dfile, 'file') ; delete(dfile); end
diary(dfile)
diary on