diff --git a/matlab/histvalf.m b/matlab/histvalf.m
index 59c98cbeefe30bc98cd358dfab5a24c04100dae4..576f5ff635150a793e3b1f5081955a23e882665a 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 bfdcab373c2d26c470312853a7f111e25ef3f245..e6cc27585de96977e3d36ef98293fb1e459eff56 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