From f7149d60cc97c784f8e151a6c1fb9b5621317b9e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 23 Sep 2021 16:21:08 +0200
Subject: [PATCH] NLS estimation: workaround for Octave random failure to read
 the generated ssr_*.m file

---
 matlab/+estimate/nls.m      | 9 ++++++++-
 matlab/+pac/+estimate/nls.m | 7 +++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/matlab/+estimate/nls.m b/matlab/+estimate/nls.m
index 96009ab7d8..a6471f3714 100644
--- a/matlab/+estimate/nls.m
+++ b/matlab/+estimate/nls.m
@@ -251,6 +251,13 @@ fprintf(fid, 'r = %s-(%s);\n', lhs, rhs);
 fprintf(fid, 's = r''*r;\n');
 fclose(fid);
 
+% Workaround for Octave bug https://savannah.gnu.org/bugs/?46282
+% Octave will randomly fail to read the ssr_* file generated in the +folder
+if isoctave
+    rename(['+' M_.fname], ['+' M_.fname '-tmp']);
+    rename(['+' M_.fname '-tmp'], ['+' M_.fname]);
+end
+
 % Create a function handle returning the sum of square residuals for a given vector of parameters.
 ssrfun = @(p) feval([M_.fname '.ssr_' eqname], p, DATA, M_, oo_);
 
@@ -456,4 +463,4 @@ if ~noprint
     dyn_table(title, preamble, afterward, oo_.nls.(eqname).pnames, ...
               {'Estimates','t-statistic','Std. Error'}, 4, ...
               [oo_.nls.(eqname).beta oo_.nls.(eqname).tstat sqrt(diag(C))]);
-end
\ No newline at end of file
+end
diff --git a/matlab/+pac/+estimate/nls.m b/matlab/+pac/+estimate/nls.m
index cb384cdf61..8dbb666516 100644
--- a/matlab/+pac/+estimate/nls.m
+++ b/matlab/+pac/+estimate/nls.m
@@ -164,6 +164,13 @@ fprintf(fid, 'r = %s-(%s);\n', lhs, rhs);
 fprintf(fid, 's = r''*r;\n');
 fclose(fid);
 
+% Workaround for Octave bug https://savannah.gnu.org/bugs/?46282
+% Octave will randomly fail to read the ssr_* file generated in the +folder
+if isoctave
+    rename(['+' M_.fname], ['+' M_.fname '-tmp']);
+    rename(['+' M_.fname '-tmp'], ['+' M_.fname]);
+end
+
 % Copy (sub)sample data in a matrix.
 DATA = data([range(1)-1, range]).data;
 
-- 
GitLab