Create Diary in a function?
    9 次查看(过去 30 天)
  
       显示 更早的评论
    
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?
0 个评论
采纳的回答
  Walter Roberson
      
      
 2022-5-6
        Return RAII from the function. Your onCleanup will not fire until the struct stops existing.
4 个评论
更多回答(0 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

