Logging cmd in multiple diary

5 ビュー (過去 30 日間)
Jeevraj Hejmady
Jeevraj Hejmady 2024 年 8 月 29 日
回答済み: Voss 2024 年 8 月 29 日
Hi,
i'm facing an issue in logging using the matlab 'diary' functionality. In the below pseudo-code i have 2 tools and both use diary to log the command window output and one is calling other. The diary off for 2nd tool is terminating the diary for the 1st tool as well due to which the remaining cmd display is not getting logged anywhere.
Here is how the logs are:
Is there a way to use 'diary' or some other functionality that would help is logging the command window output for the beow example ?
logger1();
Initialzing logger 1 Display Message 1 Initialzing logger 2 Display Message 1 End Log Display Message 2 End Log
function logger1()
logName = fullfile(pwd, 'log1.txt');
diary(logName);
fprintf('Initialzing logger 1\n');
fprintf('Display Message 1\n');
logger2;
fprintf('Display Message 2\n');
fprintf('End Log\n');
diary off
end
function logger2()
logName = fullfile(pwd, 'log2.txt');
diary(logName);
fprintf('Initialzing logger 2\n');
fprintf('Display Message 1\n');
fprintf('End Log\n');
diary off
end

回答 (2 件)

Mario Malic
Mario Malic 2024 年 8 月 29 日

Voss
Voss 2024 年 8 月 29 日
You can restore diary logging to log1.txt after logger2 finishes:
logger1();
function logger1()
logName = fullfile(pwd, 'log1.txt');
diary(logName);
fprintf('Initialzing logger 1\n');
fprintf('Display Message 1\n');
logger2;
diary(logName); % <- restore diary logging to log1.txt
fprintf('Display Message 2\n');
fprintf('End Log\n');
diary off
end
function logger2()
logName = fullfile(pwd, 'log2.txt');
diary(logName);
fprintf('Initialzing logger 2\n');
fprintf('Display Message 1\n');
fprintf('End Log\n');
diary off
end
Of course, another option is to fprintf to the particular file you want to write to instead of fprintf-ing to the command window and using diary.

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by