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.
0 个评论
回答(2 个)
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')
0 个评论
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!