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

1 view (last 30 days)
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.

Answers (2)

Eric Delgado
Eric Delgado on 1 Oct 2022
Edited: Eric Delgado on 1 Oct 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 on 1 Oct 2022
Edited: dpb on 1 Oct 2022
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.




Community Treasure Hunt

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

Start Hunting!