フィルターのクリア

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

56 ビュー (過去 30 日間)
Claire Fang
Claire Fang 2021 年 7 月 23 日
コメント済み: Mitesh 2022 年 3 月 24 日
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 件のコメント
Claire Fang
Claire Fang 2021 年 7 月 26 日
Thanks a lot. It worked!
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 Center および File ExchangeEntering Commands についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by