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.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 12 de Mayo de 2019

3 votos

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

5 comentarios

Andrew Robison
Andrew Robison el 26 de Jun. de 2019
I tried this, but the file is still not deleted. It doesn't have correct permissions.
Walter Roberson
Walter Roberson el 26 de Jun. de 2019
If you do not have permissions to delete the file, there is not much that MATLAB can do about that.
Andrew Robison
Andrew Robison el 26 de Jun. de 2019
My warning is:
Warning: File not found or permission denied
The file is clearly in the directory, but I can only delete the file after closing MATLAB.
Walter Roberson
Walter Roberson el 26 de Jun. de 2019
It would probably be a good idea to fully qualify the diary file name, in case you are not in the same active directory that you think you are.
Jimmy Neutron
Jimmy Neutron el 29 de Oct. de 2020
@Andrew Robison If you add
diary off
at the end of your .m file, everything works fine

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by