From 295f8d53f0f37f43af03079d282a59e8b7dc7c0e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 7 Apr 2014 14:58:11 +0200
Subject: [PATCH] smoother2histval, histval_file: fix compatibility problems
 with MATLAB.

---
 matlab/histvalf.m         |  4 +++-
 matlab/smoother2histval.m | 12 ++++++++----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/matlab/histvalf.m b/matlab/histvalf.m
index 59c98cbeef..576f5ff635 100644
--- a/matlab/histvalf.m
+++ b/matlab/histvalf.m
@@ -51,7 +51,9 @@ for i = 1:length(outvars)
         % Lagged endogenous or exogenous, search through aux vars
         undidx = find(ov_ == '_', 1, 'last'); % Index of last underscore in name
         ov = ov_(1:(undidx-1));
-        lead_lag = str2num(ov_((undidx+1):end));
+        lead_lag = ov_((undidx+1):end);
+        lead_lag = regexprep(lead_lag,'l','-');
+        lead_lag = str2num(lead_lag);
         j = [];
         for i = 1:length(M_.aux_vars)
             if M_.aux_vars(i).type ~= 1 && M_.aux_vars(i).type ~= 3
diff --git a/matlab/smoother2histval.m b/matlab/smoother2histval.m
index bfdcab373c..e6cc27585d 100644
--- a/matlab/smoother2histval.m
+++ b/matlab/smoother2histval.m
@@ -59,7 +59,8 @@ else
 end
 
 % Hack to determine if oo_.SmoothedVariables was computed after a Metropolis
-if isstruct(getfield(smoothedvars, fieldnames(smoothedvars){1}))
+tmp = fieldnames(smoothedvars);
+if isstruct(getfield(smoothedvars, tmp{1}))
     post_metropolis = 1;
 else
     post_metropolis = 0;
@@ -99,7 +100,8 @@ else
 end
 
 % Determine number of periods
-n = size(getfield(smoothedvars, fieldnames(smoothedvars){1}));
+tmp = fieldnames(smoothedvars);
+n = size(getfield(smoothedvars, tmp{1}));
 
 if n < M_.maximum_endo_lag
     error('Not enough observations to create initial conditions')
@@ -196,8 +198,10 @@ for i = 1:length(M_.aux_vars)
             j = M_.aux_vars(i).endo_index;
             M_.endo_histval(j, :) = v;
         else
-            % When saving to a file, x(-2) is in the variable called "x_-2"
-            o = setfield(o, [ orig_var '_' num2str(l) ], v);
+            % When saving to a file, x(-2) is in the variable called "x_l2"
+            lead_lag = num2str(l);
+            lead_lag = regexprep(lead_lag, '-', 'l');
+            o = setfield(o, [ orig_var '_' lead_lag ], v);
         end
     end
 end
-- 
GitLab