From d7cf8ac76df9dc5227895d27c604b7c15b0ad80f Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Thu, 24 Oct 2019 11:28:34 +0200
Subject: [PATCH] make column_names work correctly for addData

---
 src/@report_data/report_data.m     |  3 +++
 src/@report_table/report_table.m   |  3 +--
 src/@report_table/writeTableFile.m | 29 +++++++++++++++++++----------
 3 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/src/@report_data/report_data.m b/src/@report_data/report_data.m
index 4f9f4a2..84683d0 100644
--- a/src/@report_data/report_data.m
+++ b/src/@report_data/report_data.m
@@ -26,6 +26,9 @@ classdef report_data < handle
         tablePrecision = ''
         zeroTol = 1e-6
     end
+    properties
+        column_names = ''
+    end
     methods
         function o = report_data(varargin)
             %function o = report_data(varargin)
diff --git a/src/@report_table/report_table.m b/src/@report_table/report_table.m
index d7418e0..adf24a4 100644
--- a/src/@report_table/report_table.m
+++ b/src/@report_table/report_table.m
@@ -22,11 +22,10 @@ classdef report_table < handle
     end
     properties (Access = private)
         series = {}
-        table_data = {}
+        table_data = {}               % The table data
         % Not documented
         preamble = {''}
         afterward = {''}
-        column_names = ''
     end
     properties (SetAccess = private)
         tableDirName = 'tmpRepDir'    % The name of the folder in which to store this table. Default: tmpRepDir.
diff --git a/src/@report_table/writeTableFile.m b/src/@report_table/writeTableFile.m
index 144f816..f103917 100644
--- a/src/@report_table/writeTableFile.m
+++ b/src/@report_table/writeTableFile.m
@@ -39,7 +39,9 @@ if ne == 0 && ~is_data_table
     warning('@report_table.write: no series to plot, returning');
     return
 end
-
+if is_data_table
+    ne = size(o.table_data{1}.data,2);
+end
 if exist([rep_dir '/' o.tableDirName], 'dir') ~= 7
     mkdir([rep_dir '/' o.tableDirName]);
 end
@@ -177,8 +179,13 @@ else
     if o.showVlines
         fprintf(fid, '|');
     end
-    for i = 1:length(o.column_names)
-        if isempty(o.column_names{i})
+    if ~isempty(o.table_data{1}.column_names) && ne ~= length(o.table_data{1}.column_names)
+        error(['@report_table.writeTableFile: when writing a data table and passing the ' ...
+            '`column_names` option, it must have the same number of elements as the ' ...
+            'number of columns in the data']);
+    end
+    for i = 1:ne
+        if isempty(o.table_data{1}.column_names) ||isempty(o.table_data{1}.column_names{i})
             fprintf(fid, 'l');
         else
             fprintf(fid, 'r');
@@ -216,14 +223,16 @@ if ~is_data_table
         csvseries.save(strrep(o.tableName, '.tex', ''), 'csv');
     end
 else
-    fprintf(fid, '%%\n');
-    fprintf(fid, '\\hline%%\n');
-    fprintf(fid, '%%\n');
-    for i = 1:length(o.column_names)
-        if ~isempty(o.column_names{i})
-            fprintf(fid, '%s', o.column_names{i});
+    if ~isempty(o.table_data{1}.column_names)
+        fprintf(fid, '%%\n');
+        fprintf(fid, '\\hline%%\n');
+        fprintf(fid, '%%\n');
+    end
+    for i = 1:ne
+        if ~isempty(o.table_data{1}.column_names) && ~isempty(o.table_data{1}.column_names{i})
+            fprintf(fid, '%s', o.table_data{1}.column_names{i});
         end
-        if i ~= length(o.column_names)
+        if i ~= ne
             fprintf(fid, ' & ');
         end
     end
-- 
GitLab