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:
  - Partial reversion of global indentation of macro processor header files introduced in e2d5a83592634f0604d8c86409748cd2ec5906d2
  - Symbol List check pass: allow caller to specify the valid types of variables in a Symbol List
  - Allow `dynasave` and `dynatype` to support exogenous variables in their var_list

issue #1691
parent 2134f261
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 ba4fd2d2e042bdeffe38d5aa967bfb7131f6a0b1
Subproject commit 98a9c8888040bd5670f0ed844432969f377206bc
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment