How to create a log_file.txt that stores my outputs and variables?

1 visualización (últimos 30 días)
Bhagii
Bhagii el 30 de Sept. de 2022
Editada: dpb el 1 de Oct. de 2022
Hello,
I want to create a log_file.txt which stores the output of a variable without overwriting the alreayd saved values. Currently, I'm utilizing the 'fopenf' and 'fprintf' functions to do that but the problem is that for a new output result it is overwriting the previous value. Please do let me know of any ideal solution for this scenario, to capture all the output values without happening to overwrite the previous one.

Respuestas (2)

Eric Delgado
Eric Delgado el 1 de Oct. de 2022
Editada: Eric Delgado el 1 de Oct. de 2022
You have a lot of possibilities - writetable OR writematrix OR writecell OR save....
See the functions documentation.
% if you have two tables T1 and T2
T1 = table([1;2;3], ["A";"B";"C"])
T2 = table([4;5;6], ["D";"E";"F"])
writetable(T1, 'myTableFile.txt', 'WriteMode', 'append')
writetable(T2, 'myTableFile.txt', 'WriteMode', 'append')
% if you have two strings S1 and S2
S1 = "First string..."
S2 = "Second string..."
writematrix(S1, 'myStringFile.txt', 'WriteMode', 'append')
writematrix(S2, 'myStringFile.txt', 'WriteMode', 'append')

dpb
dpb el 1 de Oct. de 2022
Editada: dpb el 1 de Oct. de 2022
Use
fid=fopen(fillfile('LogFileBaseLocation','LogFileName.txt','a+');
to open the file for append, read/write access. See fopen doc for details on permissions, example code, etc.
Or, if the log is within one session and you're just opening/closing the file every time between writing, open it at the beginning and don't close until done.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by