Skip to content
Snippets Groups Projects
Verified Commit e892a86f authored by Houtan Bastani's avatar Houtan Bastani
Browse files

support saving exogenous variables in `dynasave`, `dynasave`; fix bugs in `dynasave`; add test

- `dynasave`: if a variable being saved was named `n` or `s`, the `eval` statements would break the code
- `dynasave`: use the `-struct` option to `save` to avoid `eval` statements
- `dynasave` and `dynatype`: do everything in 1 loop instead of 2
- `dynasave` and `dynatype`: use `strcmp` instead of `strfind`

- preprocessor update contains:
  - Allow `dynasave` and `dynatype` to support exogenous variables in their var_list

issue #1691

(cherry picked from commit bf102030)
parent e172d627
No related branches found
No related tags found
1 merge request!1815WIP Cherry-picks for 4.6
Pipeline #2785 passed
function dynasave(s, var_list) function dynasave(s,var_list)
% function dynasave(s,var_list) % function dynasave(s,var_list)
% This optional command saves the simulation results in a .MAT file. % This command saves the simulation results in a .MAT file.
% %
% INPUTS % INPUTS
% s: filename % s: filename
...@@ -12,7 +12,7 @@ function dynasave(s, var_list) ...@@ -12,7 +12,7 @@ function dynasave(s, var_list)
% SPECIAL REQUIREMENTS % SPECIAL REQUIREMENTS
% none % none
% Copyright (C) 2001-2018 Dynare Team % Copyright (C) 2001-2020 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
...@@ -39,20 +39,19 @@ if ~isfield(oo_, 'endo_simul') || isempty(oo_.endo_simul) ...@@ -39,20 +39,19 @@ if ~isfield(oo_, 'endo_simul') || isempty(oo_.endo_simul)
error('dynasave:: The results structure does not contain simulated series. Maybe the periods option has not been set.') error('dynasave:: The results structure does not contain simulated series. Maybe the periods option has not been set.')
end end
n = length(var_list); for i = 1:length(var_list)
ivar = zeros(n, 1); idx = strcmp(var_list{i}, M_.endo_names);
for i=1:n if any(idx)
i_tmp = strmatch(var_list{i}, M_.endo_names, 'exact'); SaveStruct.(var_list{i}) = oo_.endo_simul(idx,:);
if isempty(i_tmp)
error ('One of the specified variables does not exist') ;
else else
ivar(i) = i_tmp; idx = strcmp(var_list{i}, M_.exo_names);
if any(idx)
SaveStruct.(var_list{i}) = oo_.exo_simul(:,idx);
else
error(['Should not arrive here: ' var_list{i} ' not found in M_.endo_names or M_.exo_names']) ;
end
end end
end end
eval([var_list{1} ' = oo_.endo_simul(ivar(1),:)'';']) save(s, '-struct', 'SaveStruct');
eval(['save ' s ' ' var_list{1} ' -mat'])
for dynare__i_ = 2:n
eval([var_list{dynare__i_} ' = oo_.endo_simul(ivar(dynare__i_),:)'';'])
eval(['save ' s ' ' var_list{dynare__i_} ' -append -mat'])
end end
function dynatype (s,var_list) function dynatype (s,var_list)
% function dynatype (s,var_list) % function dynatype (s,var_list)
% This optional command saves the simulation results in a text file. The name of each % This command saves the simulation results in a text file. The name of each
% variable preceeds the corresponding results. This command must follow SIMUL. % variable preceeds the corresponding results. This command must follow SIMUL.
% %
% INPUTS % INPUTS
...@@ -13,7 +13,7 @@ function dynatype (s,var_list) ...@@ -13,7 +13,7 @@ function dynatype (s,var_list)
% SPECIAL REQUIREMENTS % SPECIAL REQUIREMENTS
% none % none
% Copyright (C) 2001-2018 Dynare Team % Copyright (C) 2001-2020 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
...@@ -32,29 +32,27 @@ function dynatype (s,var_list) ...@@ -32,29 +32,27 @@ function dynatype (s,var_list)
global M_ oo_ global M_ oo_
fid=fopen(s,'w') ; fid = fopen(s, 'w');
if isempty(var_list) if isempty(var_list)
var_list = M_.endo_names(1:M_.orig_endo_nbr); var_list = M_.endo_names(1:M_.orig_endo_nbr);
end end
n = length(var_list); for i = 1:length(var_list)
ivar = zeros(n,1); idx = strcmp(var_list{i}, M_.endo_names);
if any(idx)
for i=1:n fprintf(fid, '%s\n', M_.endo_names{idx});
i_tmp = strmatch(var_list{i}, M_.endo_names, 'exact'); fprintf(fid, '%15.8g\n', oo_.endo_simul(idx,:)');
if isempty(i_tmp)
error ('One of the specified variables does not exist') ;
else else
ivar(i) = i_tmp; idx = strcmp(var_list{i}, M_.exo_names);
if any(idx)
fprintf(fid, '%s\n', M_.exo_names{idx});
fprintf(fid, '%15.8g\n', oo_.exo_simul(:,idx));
else
error(['Should not arrive here: ' var_list{i} ' not found in M_.endo_names or M_.exo_names']) ;
end
end end
end end
for i = 1:n fclose(fid);
fprintf(fid,M_.endo_names{ivar(i)},'\n') ;
fprintf(fid,'\n') ;
fprintf(fid,'%15.8g\n',oo_.endo_simul(ivar(i),:)') ;
end end
fclose(fid) ;
return ;
Subproject commit c23991017187b342b88bbc7684576182791b2658 Subproject commit da4ca9e32386618487a86ba8429cc0f9a26c9629
...@@ -36,6 +36,9 @@ end; ...@@ -36,6 +36,9 @@ end;
simul(periods=200); simul(periods=200);
dynasave('myfile') c x k;
dynatype('myfile1.txt') c x k;
rplot c; rplot c;
rplot k; rplot k;
rplot dc; rplot dc;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment