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
Branches
Tags
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)
% This optional command saves the simulation results in a .MAT file.
% This command saves the simulation results in a .MAT file.
%
% INPUTS
% s: filename
......@@ -12,7 +12,7 @@ function dynasave(s, var_list)
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2001-2018 Dynare Team
% Copyright (C) 2001-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -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.')
end
n = length(var_list);
ivar = zeros(n, 1);
for i=1:n
i_tmp = strmatch(var_list{i}, M_.endo_names, 'exact');
if isempty(i_tmp)
error ('One of the specified variables does not exist') ;
for i = 1:length(var_list)
idx = strcmp(var_list{i}, M_.endo_names);
if any(idx)
SaveStruct.(var_list{i}) = oo_.endo_simul(idx,:);
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
eval([var_list{1} ' = oo_.endo_simul(ivar(1),:)'';'])
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'])
save(s, '-struct', 'SaveStruct');
end
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.
%
% INPUTS
......@@ -13,7 +13,7 @@ function dynatype (s,var_list)
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2001-2018 Dynare Team
% Copyright (C) 2001-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -32,29 +32,27 @@ function dynatype (s,var_list)
global M_ oo_
fid=fopen(s,'w') ;
fid = fopen(s, 'w');
if isempty(var_list)
var_list = M_.endo_names(1:M_.orig_endo_nbr);
end
n = length(var_list);
ivar = zeros(n,1);
for i=1:n
i_tmp = strmatch(var_list{i}, M_.endo_names, 'exact');
if isempty(i_tmp)
error ('One of the specified variables does not exist') ;
for i = 1:length(var_list)
idx = strcmp(var_list{i}, M_.endo_names);
if any(idx)
fprintf(fid, '%s\n', M_.endo_names{idx});
fprintf(fid, '%15.8g\n', oo_.endo_simul(idx,:)');
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
for i = 1:n
fprintf(fid,M_.endo_names{ivar(i)},'\n') ;
fprintf(fid,'\n') ;
fprintf(fid,'%15.8g\n',oo_.endo_simul(ivar(i),:)') ;
fclose(fid);
end
fclose(fid) ;
return ;
Subproject commit c23991017187b342b88bbc7684576182791b2658
Subproject commit da4ca9e32386618487a86ba8429cc0f9a26c9629
......@@ -36,6 +36,9 @@ end;
simul(periods=200);
dynasave('myfile') c x k;
dynatype('myfile1.txt') c x k;
rplot c;
rplot k;
rplot dc;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment