Create Diary in a function?

조회 수: 9 (최근 30일)
Florian Rössing
Florian Rössing 2022년 5월 6일
댓글: Walter Roberson 2022년 5월 9일
I am currently working on a simulation project where I want progress logs to be written to a file, so I can inspect them later. I was using diary for this. However having the setup of diary in the main function look ugly and made it bulky, so I wanted to move all the setup and diary functionality to a seperate function alike this:
function createLogFile(RunPath)
diaryfile = append(RunPath, 'log.txt');
if exist(diaryfile, 'file')
delete(diaryfile);
end
diary(diaryfile);
RAII.diary = onCleanup(@() diary('off'));
diary on
end
However as soon as the function finishes the diary falls out of scope and is closed.
Is there a way to pass the diary out of the function?

채택된 답변

Walter Roberson
Walter Roberson 2022년 5월 6일
Return RAII from the function. Your onCleanup will not fire until the struct stops existing.
  댓글 수: 4
Florian Rössing
Florian Rössing 2022년 5월 9일
Thanks for that nice explanation. I believed taht RAII was some kind of system object that specifically has this attribute. The oddly specific and strange name led me to believe that.
Walter Roberson
Walter Roberson 2022년 5월 9일
https://en.cppreference.com/w/cpp/language/raii sounds plausible

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by