From ee7f2fb7d06a2e74cd71144aeadd5f21f6bf92d6 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 2 Apr 2013 15:00:00 +0200 Subject: [PATCH] reporting: @series support writing colors/marking table rows --- matlab/reporting/@series/series.m | 4 ++ matlab/reporting/@series/write.m | 74 +++++++++++++++++++++++++++++++ matlab/reporting/@table/write.m | 28 ++---------- 3 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 matlab/reporting/@series/write.m diff --git a/matlab/reporting/@series/series.m b/matlab/reporting/@series/series.m index 72922fdf7..0be99a830 100644 --- a/matlab/reporting/@series/series.m +++ b/matlab/reporting/@series/series.m @@ -44,6 +44,10 @@ o.graph_marker_edge_color = 'auto'; o.graph_marker_face_color = 'auto'; o.graph_marker_size = 6; +o.table_markers = false; +o.table_neg_color = 'red'; +o.table_pos_color = 'blue'; + if nargin == 1 assert(isa(varargin{1}, 'series'),['@series.series: with one arg you ' ... 'must pass a series object']); diff --git a/matlab/reporting/@series/write.m b/matlab/reporting/@series/write.m new file mode 100644 index 000000000..5ef2082dc --- /dev/null +++ b/matlab/reporting/@series/write.m @@ -0,0 +1,74 @@ +function o = write(o, fid, dates, precision) +%function o = write(o, fid, dates, precision) +% Write Table Row +% +% INPUTS +% o [series] series object +% xrange [dynDates] range of x values for line +% +% OUTPUTS +% o [series] series object +% +% SPECIAL REQUIREMENTS +% none + +% Copyright (C) 2013 Dynare Team +% +% This file is part of Dynare. +% +% Dynare is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Dynare. If not, see . + +%% Validate options passed to function +assert(fid ~= -1); +assert(isa(dates, 'dynDates')); +assert(isint(precision)); + +%% Validate options provided by user +assert(~isempty(o.data) && isa(o.data, 'dynSeries'), ... + '@series.write: must provide data as a dynSeries'); + +assert(ischar(o.color), '@series.write: color must be a string'); +assert(ischar(o.table_neg_color), '@series.write: table_neg_color must be a string'); +assert(ischar(o.table_pos_color), '@series.write: table_pos_color must be a string'); +assert(islogical(o.table_markers), '@series.write: table_markers must be a string'); + +%% Write Output +dataString = ['%.' num2str(precision) 'f']; +precision = 10^precision; + +fprintf(fid, '%% Table Row (series)\n'); +fprintf(fid, '%s', o.data.name{:}); +data = o.data(dates); +data = data.data; +for i=1:size(data,1) + thisCellData = round(data(i)*precision)/precision; + + fprintf(fid, ' &'); + if o.table_markers + if thisCellData < 0 + fprintf(fid, '\\color{%s}', o.table_neg_color); + elseif thisCellData > 0 + fprintf(fid, '\\color{%s}', o.table_pos_color); + end + fprintf(fid, '['); + end + + fprintf(fid, dataString, thisCellData); + + if o.table_markers + fprintf(fid, ']'); + end +end +fprintf(fid, ' \\\\\n\n'); +end diff --git a/matlab/reporting/@table/write.m b/matlab/reporting/@table/write.m index 1571b7068..1a879835a 100644 --- a/matlab/reporting/@table/write.m +++ b/matlab/reporting/@table/write.m @@ -49,16 +49,6 @@ else end ndates = dates.ndat; -ne = o.seriesElements.numElements(); -ds = dynSeries(); -for i=1:ne - if isempty(ds) - ds = o.seriesElements(i).getData(dates); - else - ds = [ds o.seriesElements(i).getData(dates)]; - end -end - disp('creating table.........'); fprintf(fid, '%% Table Object\n'); fprintf(fid, '\\setlength{\\tabcolsep}{4pt}\n'); @@ -126,20 +116,10 @@ end fprintf(fid, '\\\\%%\n'); fprintf(fid, '%%\n'); -% Table Data -vars = ds.name; -nvars = size(vars); -data = ds.data; -assert(isint(o.precision)); -precision = 10^o.precision; -dataString = [' & %.' num2str(o.precision) 'f']; -for i=1:nvars - fprintf(fid, '%% Table Row %d\n', i); - fprintf(fid, '%s', vars{i}); - for j=1:ndates - fprintf(fid, dataString, round(data(j,i)*precision)/precision); - end - fprintf(fid, ' \\\\\n\n'); +% Write Table Data +ne = o.seriesElements.numElements(); +for i=1:ne + o.seriesElements(i).write(fid, dates, o.precision); end fprintf(fid, '\\bottomrule\n');