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

1 次查看(过去 30 天)
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.

回答(2 个)

Eric Delgado
Eric Delgado 2022-10-1
编辑:Eric Delgado 2022-10-1
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 2022-10-1
编辑:dpb 2022-10-1
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.

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by