fprintf to print to both file and command window

59 ビュー (過去 30 日間)
Louis
Louis 2020 年 4 月 29 日
コメント済み: Louis 2020 年 5 月 2 日
I'd like to run a single fprintf operation to print to both command window and file.
I know that the below command only print to 'log.txt' file:
fid = fopen('log.txt', 'w');
fprintf(fid, 'start logging\n');
And below will only print to command window:
fprintf('start logging\n');
Is there a way to print to both without calling fprintf twice?
Thanks,

採用された回答

the cyclist
the cyclist 2020 年 4 月 29 日
I doubt it.
I'm not sure what your goal is, but it would certainly be trivial to write your own small function. Something like
function [] = writeToScreenAndFile(fid,str)
fprintf(fid, str);
fprintf(str);
end
  1 件のコメント
Louis
Louis 2020 年 4 月 29 日
The goal is to have a log file which logs desired stutus messages. However, I also would like the log messages to appear on the command window as well.

サインインしてコメントする。

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 29 日
編集済み: Ameer Hamza 2020 年 4 月 29 日
Read my answer here: https://www.mathworks.com/matlabcentral/answers/516548-using-fprintf-to-write-to-multiple-files-simultaneously#answer_424960 which answers this exact question. It suggests two methods: create a custom function or overloading fprintf function.

カテゴリ

Help Center および File ExchangeScripts についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by