diff --git a/matlab/dyn_forecast.m b/matlab/dyn_forecast.m
index 812ab1ca4461a79788308304406286c55d65416e..1804b9a67887f1fab7c607e98b8f60c2fb0fa315 100644
--- a/matlab/dyn_forecast.m
+++ b/matlab/dyn_forecast.m
@@ -87,8 +87,10 @@ switch task
                 i_var_obs = [ i_var_obs; tmp];
                 trend_coeffs = [trend_coeffs; oo_.Smoother.TrendCoeffs(i)];
             end
-        end         
-        trend = trend_coeffs*(gend+(1-M_.maximum_lag:horizon));
+        end
+        if ~isempty(trend_coeffs) 
+          trend = trend_coeffs*(gend+(1-M_.maximum_lag:horizon)); 
+        end
     end
     global bayestopt_
     if isfield(bayestopt_,'mean_varobs')