reporting: annualAverages option for addTable

remove-priordens
Houtan Bastani 2013-09-26 16:34:36 +02:00
parent f10dbf60c6
commit 376c605499
3 changed files with 45 additions and 6 deletions

View File

@ -1,5 +1,5 @@
function o = write(o, fid, dates, precision)
%function o = write(o, fid, dates, precision)
function o = write(o, fid, dates, precision, yrsForAvgs)
%function o = write(o, fid, dates, precision, yrsForAvgs)
% Write Table Row
%
% INPUTS
@ -7,6 +7,8 @@ function o = write(o, fid, dates, precision)
% fid [int] file id
% dates [dynDates] dates for series slice
% precision [float] precision with which to print the data
% yrsForAvgs [bool] the years for which to compute averages
%
%
% OUTPUTS
% o [series] series object
@ -92,5 +94,26 @@ for i=1:size(data,1)
fprintf(fid, ']');
end
end
% Calculate and display annual averages
for i=1:length(yrsForAvgs)
slice = o.data(dynDate([num2str(yrsForAvgs(i)) 'q1']):dynDate([num2str(yrsForAvgs(i)) 'q4']));
avg = sum(slice.data)/size(slice);
fprintf(fid, '&');
if o.tableShowMarkers
if avg < -o.tableMarkerLimit
fprintf(fid, '\\color{%s}', o.tableNegColor);
elseif avg > o.tableMarkerLimit
fprintf(fid, '\\color{%s}', o.tablePosColor);
end
fprintf(fid, '[');
end
fprintf(fid, dataString, round(avg*precision)/precision);
if o.tableShowMarkers
fprintf(fid, ']');
end
end
fprintf(fid, '\\\\%%\n');
end

View File

@ -41,6 +41,8 @@ o.showVlines = false;
o.vlineAfter = '';
o.vlineAfterEndOfPeriod = false;
o.annualAverages = false;
o.data = '';
o.seriesToUse = '';
o.range = {};
@ -73,6 +75,7 @@ end
% Check options provided by user
assert(ischar(o.title), '@table.table: title must be a string');
assert(islogical(o.annualAverages), '@table.table: annualAverages must be true or false');
assert(islogical(o.showHlines), '@table.table: showHlines must be true or false');
assert(islogical(o.showVlines), '@table.table: showVlines must be true or false');
assert(isint(o.precision), '@table.table: precision must be an int');

View File

@ -72,6 +72,19 @@ for i=1:ndates
end
end
end
datedata = dates.time;
years = unique(datedata(:, 1));
if o.annualAverages
yrsForAvgs = years;
else
yrsForAvgs = [];
end
for i=1:length(yrsForAvgs)
fprintf(fid, 'r');
if o.showVlines
fprintf(fid, '|');
end
end
fprintf(fid, '@{}}%%\n');
if ~isempty(o.title)
fprintf(fid, '\\multicolumn{%d}{c}{\\%s %s}\\\\\n', ...
@ -80,8 +93,6 @@ end
fprintf(fid, '\\toprule%%\n');
% Column Headers
datedata = dates.time;
years = unique(datedata(:, 1));
thdr = num2cell(years, size(years, 1));
switch dates.freq
case 1
@ -101,7 +112,6 @@ switch dates.freq
end
end
end
for i=1:size(thdr, 1)
fprintf(fid, ' & \\multicolumn{%d}{c}{%d}', size(thdr{i,2}, 2), thdr{i,1});
end
@ -121,6 +131,9 @@ switch dates.freq
otherwise
error('@table.write: invalid dynSeries frequency');
end
for i=1:length(yrsForAvgs)
fprintf(fid, ' & %d', years(i));
end
fprintf(fid, '\\\\[-2pt]%%\n');
fprintf(fid, '\\hline%%\n');
fprintf(fid, '%%\n');
@ -128,7 +141,7 @@ fprintf(fid, '%%\n');
% Write Table Data
ne = o.seriesElements.numSeriesElements();
for i=1:ne
o.seriesElements(i).write(fid, dates, o.precision);
o.seriesElements(i).write(fid, dates, o.precision, yrsForAvgs);
if o.showHlines
fprintf(fid, '\\hline\n');
end