From e892a86f3f7437ede1e20753e30ea12d90fc51bf Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Mon, 6 Jan 2020 12:01:41 +0100
Subject: [PATCH] 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 bf102030cbddeb4e2a405e75efa3dfdc2be6dc53)
---
 matlab/dynasave.m | 29 ++++++++++++++---------------
 matlab/dynatype.m | 34 ++++++++++++++++------------------
 preprocessor      |  2 +-
 tests/ramst2.mod  |  3 +++
 4 files changed, 34 insertions(+), 34 deletions(-)

diff --git a/matlab/dynasave.m b/matlab/dynasave.m
index 45642a8ca8..d80aa98abc 100644
--- a/matlab/dynasave.m
+++ b/matlab/dynasave.m
@@ -1,6 +1,6 @@
-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
diff --git a/matlab/dynatype.m b/matlab/dynatype.m
index 33f3182677..c0fe8c4d3f 100644
--- a/matlab/dynatype.m
+++ b/matlab/dynatype.m
@@ -1,6 +1,6 @@
 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 ;
diff --git a/preprocessor b/preprocessor
index c239910171..da4ca9e323 160000
--- a/preprocessor
+++ b/preprocessor
@@ -1 +1 @@
-Subproject commit c23991017187b342b88bbc7684576182791b2658
+Subproject commit da4ca9e32386618487a86ba8429cc0f9a26c9629
diff --git a/tests/ramst2.mod b/tests/ramst2.mod
index be2fe7a643..a9f92ccb2e 100644
--- a/tests/ramst2.mod
+++ b/tests/ramst2.mod
@@ -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;
-- 
GitLab