diff --git a/matlab/dyn_forecast.m b/matlab/dyn_forecast.m
index 135e0eb9b3e3b62c72ef485a35fca8040fda05c6..8afc40f2bc24a333653356b47bbab86b0067215b 100644
--- a/matlab/dyn_forecast.m
+++ b/matlab/dyn_forecast.m
@@ -107,9 +107,10 @@ switch task
         trend_coeffs = [];
         for i=1:length(var_obs)
             tmp = strmatch(var_obs{i},endo_names(i_var,:),'exact');
+            trend_var_index=strmatch(var_obs{i},M.endo_names,'exact');
             if ~isempty(tmp)
                 i_var_obs = [ i_var_obs; tmp];
-                trend_coeffs = [trend_coeffs; oo.Smoother.TrendCoeffs(i)];
+                trend_coeffs = [trend_coeffs; oo.Smoother.TrendCoeffs(trend_var_index)];
             end
         end
         if ~isempty(trend_coeffs)