필터 지우기
필터 지우기

How to create a log file of output regardless of whether program finishes running?

조회 수: 56 (최근 30일)
How can I record a log-file of Matlab console output even if my program doesn’t finish running because of errors? I used “diary on” and “diary off” to log the output, but since the program broke out prematurely, “diary off” was not run, and therefore the log file was not created. An example is below. In this example, the fmincon stops when some parameters travel to certain regions. Once the whole program stops, "diary off" doesn't get to run, and I have no output log file. The key is that I still want to save the parameters and function values from the past iterations of fmincon into a log file, and I don’t want to manually run “diary off” every time. Does anyone have a solution to this?
clear;
diary_name = strcat(datestr(now),'_diary.txt');
diary_folder = pwd;
diary(diary_name)
disp([datestr(now), ' Main Program'])
global theta1 predicted gmmresid
load('outervariables.mat');
theta1 = zeros(size(outstruct.x1, 2),1);
spmd
data = load(['dataworker' num2str(labindex) '.mat']);
end
theta2 = sig;
fun = @(theta2)gmmobj(theta2, outstruct);
maxiter = 1000;
options = optimoptions('fmincon', ...
'SpecifyObjectiveGradient',true, 'Display', 'iter-detailed', ...
'MaxFunctionEvaluations', maxiter, ...
'OptimalityTolerance', 1e-5, 'FunctionTolerance', 1e-5);
theta20 = theta2;
lb = [0; 0; 0 ; 0; 0; -6; 0; 0; 0.00001; -5; 0];
ub = [1; 0.1; 0.1; 0.2; 0.5; 0; 4; 0.5; 0.1; 0; 4];
disp('IVs for theta2 are not demeaned')
disp(['Minimizing GMM objective function with a max of ', num2str(maxiter), ' interations returns: '])
tic
[x, fval] = fmincon(fun, theta20, [], [], [], [], lb, ub, [], options);
toc
disp('theta2 estimates are')
x
diary off;
save('blpresults.mat', 'x', 'fval', 'predicted', 'gmmresid', 'theta1');

채택된 답변

Chunru
Chunru 2021년 7월 24일
Consider use error catch:
clear;
try
diary_name = strcat('_diary.txt');
diary_folder = pwd;
diary(diary_name)
disp([datestr(now), ' Main Program'])
global theta1 predicted gmmresid
load('outervariables.mat');
theta1 = zeros(size(outstruct.x1, 2),1);
spmd
data = load(['dataworker' num2str(labindex) '.mat']);
end
theta2 = sig;
fun = @(theta2)gmmobj(theta2, outstruct);
maxiter = 1000;
options = optimoptions('fmincon', ...
'SpecifyObjectiveGradient',true, 'Display', 'iter-detailed', ...
'MaxFunctionEvaluations', maxiter, ...
'OptimalityTolerance', 1e-5, 'FunctionTolerance', 1e-5);
theta20 = theta2;
lb = [0; 0; 0 ; 0; 0; -6; 0; 0; 0.00001; -5; 0];
ub = [1; 0.1; 0.1; 0.2; 0.5; 0; 4; 0.5; 0.1; 0; 4];
disp('IVs for theta2 are not demeaned')
disp(['Minimizing GMM objective function with a max of ', num2str(maxiter), ' interations returns: '])
tic
[x, fval] = fmincon(fun, theta20, [], [], [], [], lb, ub, [], options);
toc
disp('theta2 estimates are')
x
catch
disp('Error occurs.')
end
diary off;
save('blpresults.mat', 'x', 'fval', 'predicted', 'gmmresid', 'theta1');
  댓글 수: 3
Mitesh
Mitesh 2022년 3월 24일
Matlab has grate build in function call 'oncleanup'. Loop up it's help.
Whenever there is an error, you can call it to close out diary, files or even close communication with equipment whenever there is an error.
I use it alot. And it is very help. Except for when it come to diary off....it's bit tracky and haven't quite figure out how to use it. i use a lot for closeing out Files and GPIB/USB communication on error.

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by