Running dynare from inside of a matlab function forces "clear all" behavior.
Created by: Ufos92
Issue: if executing dynare command from inside of a function, all variables that were created inside this function are purged regardless clearall option. I am not sure, whether this is a dynare bug or a Matlab problem, but may be you at least would like to add this to documentation. Here is a test-case: (github does not support upload, so you will have to copypaste the code...)
------------ reproduce_bug.m ------------
clear all % so I create a variable alpha_p for the parameter alpha and try to pass it % to dynare. The mod file contains following line in the parameter section: % alpha = alpha_p;
% normal call to dynare: clear all alpha_p = 0.33 dynare first_one %noclearall % doesn't work.
% noclearall option clear all alpha_p = 0.33 dynare first_one noclearall %works
% via function clear all run_dynare_param(0.33, 'first_one ', 'noclearall') % does not work
% via function, but defining alpha_p outside clear all alpha_p = 0.33 run_dynare_param(0.33, 'first_one ', 'noclearall')
------------ run_dynare_param.m ------------
function [out] = run_dynare_param(param_alpha, mod_file, options) % let's just call dynare with some options and custom parameter alpha
% defaults if ~exist('options','var') options = '' end
% keep dynare output global M_ options_ oo_
% set alpha global alpha_p alpha_p = 0.33
% run dynare eval(['dynare ', mod_file, ' ', options])
% save and return out.M = M_; out.options = options_; out.oo = oo_; return
------------ first_one.mod ------------
// copypasted from dynare's guide
// ----- Preamble ----- var k l z y c i w r;
varexo e;
parameters beta psi delta alpha rho sigma epsilon; alpha = alpha_p; beta = 0.98; delta = 0.023; psi = 1.75; rho = 0.95; sigma = (0.007/(1-alpha)); epsilon = 10;
// ----- Model ----- model; (1/c) = beta_(1/c(+1))(1+r(+1)-delta); psi_c/(1-l) = w; c+i = y; z = rho_z(-1)+e; y = (k(-1)^alpha)(exp(z)l)^(1-alpha); w = y((epsilon-1)/epsilon)(1-alpha)/l; r = y((epsilon-1)/epsilon)_alpha/k(-1); i = k-(1-delta)*k(-1); end;
// ----- Steady State -----
// almost the exact steady state initval; k = 9; c = 0.7; l = 0.3; w = 2.0; r = 0; z = 0; e = 0; end;
// calculate steady state and set it as the starting point for impulse responce functions steady;
// Blanchard-Kahn condition check check;
// ----- Impulse-Responce -----
// shocks shocks; var e; stderr 1; end;
// simulate and plot // stoch_simul(order = 1) k l z y c i;