From 6cbe459ac842b298cb3f65faefa1e82800e2b31d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?=
 <stepan@adjemian.eu>
Date: Thu, 5 Dec 2019 11:57:49 +0100
Subject: [PATCH] Efficiency change. Store a single matrix of data instead of
 vectors.

Normally it should still be possible to instantiate dseries objects
from mat files generated by previous versions of Dynare (before 4.6).
---
 src/@dseries/save.m           |  8 ++------
 src/read/load_mat_file_data.m | 35 +++++++++++++++++++++--------------
 2 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/src/@dseries/save.m b/src/@dseries/save.m
index 8f4bb0f..a7520fd 100644
--- a/src/@dseries/save.m
+++ b/src/@dseries/save.m
@@ -103,16 +103,12 @@ switch format
     TEX__ = o.tex;
     OPS__ = o.ops;
     TAGS__ = o.tags;
-    str = [];
-    for v = 1:vobs(o)
-        str = sprintf('%s %s = o.data(:,%u);', str, o.name{v}, v);
-    end
-    eval(str);
+    DATA__ = o.data;
     currentdirectorycontent = dir();
     if ismember([basename, '.mat'], {currentdirectorycontent.name})
         copyfile([basename, '.mat'], [basename, '.old.mat']);
     end
-    save([basename '.mat'], 'INIT__', 'FREQ__', 'NAMES__', 'TEX__', 'OPS__', 'TAGS__', o.name{:});
+    save([basename '.mat'], 'INIT__', 'FREQ__', 'NAMES__', 'TEX__', 'OPS__', 'TAGS__', 'DATA__');
   case 'csv'
     currentdirectorycontent = dir();
     if ismember([basename, '.csv'],{currentdirectorycontent.name})
diff --git a/src/read/load_mat_file_data.m b/src/read/load_mat_file_data.m
index 23a9b32..da7f532 100644
--- a/src/read/load_mat_file_data.m
+++ b/src/read/load_mat_file_data.m
@@ -85,21 +85,28 @@ else
     tags = struct();
 end
 
-data = [];
-if isempty(varlist)
-    varlist = fieldnames(datafile);
-end
-
-for i=1:length(varlist)
-    try
-        tmp = datafile.(varlist{i});
-        if isvector(tmp)
-            data = [data,  tmp(:)];
-        else
-            error('load_mat_file:: All the variables must be vectors (%s is not a vector)!', varlist{i})
+if isfield(datafile,'DATA__')
+    data = datafile.DATA__;
+    datafile = rmfield(datafile, 'DATA__');
+else
+    % Previously to dynare 4.6 variables were stored as vectors of
+    % common length in the mat file. This part of the code deals
+    % with mat files generated by these older versions of Dynare.
+    data = [];
+    if isempty(varlist)
+        varlist = fieldnames(datafile);
+    end
+    for i=1:length(varlist)
+        try
+            tmp = datafile.(varlist{i});
+            if isvector(tmp)
+                data = [data,  tmp(:)];
+            else
+                error('load_mat_file:: All the variables must be vectors (%s is not a vector)!', varlist{i})
+            end
+        catch
+            error('load_mat_file:: All the vectors (variables) in %s must have the same number of rows (observations)!', inputname(1))
         end
-    catch
-        error('load_mat_file:: All the vectors (variables) in %s must have the same number of rows (observations)!', inputname(1))
     end
 end
 
-- 
GitLab