write.m 3.61 KB
Newer Older
Houtan Bastani's avatar
Houtan Bastani committed
1
2
3
function o = write(o, fid)
%function o = write(o, fid)
% Write a Table object
Houtan Bastani's avatar
Houtan Bastani committed
4
5
%
% INPUTS
Houtan Bastani's avatar
Houtan Bastani committed
6
%   o   - Table Object
Houtan Bastani's avatar
Houtan Bastani committed
7
%   fid - int, file id
Houtan Bastani's avatar
Houtan Bastani committed
8
9
%
% OUTPUTS
Houtan Bastani's avatar
Houtan Bastani committed
10
%   o   - Table Object
Houtan Bastani's avatar
Houtan Bastani committed
11
12
13
%
% SPECIAL REQUIREMENTS
%   none
Houtan Bastani's avatar
Houtan Bastani committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

% 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 <http://www.gnu.org/licenses/>.

Houtan Bastani's avatar
Houtan Bastani committed
32
33
34
35
assert(fid ~= -1);
if isempty(o.data)
    return
end
Houtan Bastani's avatar
Houtan Bastani committed
36

Houtan Bastani's avatar
Houtan Bastani committed
37
38
39
40
41
42
%number of left-hand columns, 1 until we allow the user to group data,
% e.g.: GDP Europe
%         GDP France
%         GDP Germany
% this example would be two lh columns, with GDP Europe spanning both
nlhc = 1;
Houtan Bastani's avatar
Houtan Bastani committed
43

Houtan Bastani's avatar
Houtan Bastani committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
disp('creating table.........');
fprintf(fid, '%% Table Object\n');
fprintf(fid, '\\begin{tabular}{l');

dates = o.data.time;
ndates = dates.ndat;

for i=1:ndates
    fprintf(fid, 'r');
end
fprintf(fid, '}%%\n');
if ~isempty(o.title)
    fprintf(fid, '\\multicolumn{%d}{c}{%s} \\\\\n', ndates+nlhc, o.title);
end
fprintf(fid, '\\toprule%%\n');

datedata = dates.time;
years = unique(datedata(:, 1));
thdr = num2cell(years, size(years, 1));
lind = nlhc;
switch dates.freq
    case 1
        for i=1:size(thdr, 1)
            fprintf(fid, ' & %d', thdr{i, 1});
        end
        fprintf(fid, '\\\\%%\n');
        for i=1:size(thdr, 1)
            rind = lind + 1;
            fprintf(fid, '\\cmidrule(l{.5em}r{.5em}){%d-%d}', lind+1, rind);
            lind = rind;
        end
    case 4
        thdr{1, 2} = datedata(:, 2)';
        if size(thdr, 1) > 1
            for i=2:size(thdr, 1)
                split = find(thdr{i-1, 2} == 4, 1, 'first');
                if isempty(split)
                    error('@table.write: Shouldn''t arrive here');
                else
                    thdr{i, 2} = thdr{i-1, 2}(split+1:end);
                    thdr{i-1, 2} = thdr{i-1, 2}(1:split);
                end
            end
        end

        for i=1:size(thdr, 1)
            fprintf(fid, ' & \\multicolumn{%d}{c}{%d}', size(thdr{i,2}, 2), thdr{i,1});
        end
        fprintf(fid, '\\\\%%\n');
        for i=1:size(thdr, 1)
            rind = lind + size(thdr{i,2}, 2);
            fprintf(fid, '\\cmidrule(l{.5em}r{.5em}){%d-%d}', lind+1, rind);
            lind = rind;
        end
        for i=1:size(thdr, 1)
            quarters = thdr{i, 2};
            for j=1:size(quarters, 2)
                fprintf(fid, ' & Q%d', quarters(j));
            end
        end
        fprintf(fid, '\\\\%%\n');
        for i=1:ndates
            fprintf(fid, '\\cmidrule(l{.5em}r{.5em}){%d-%d}', i+nlhc, i+nlhc);
        end
    case 12
    otherwise
        error('@table.write: invalid dynSeries Dates');
end
fprintf(fid, '%%\n');

vars = o.data.name;
nvars = size(vars);
data = o.data.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');
end

fprintf(fid, '\\bottomrule%%\n');
fprintf(fid, '\\end{tabular}%%\n');
fprintf(fid, '%% End Table Object\n');
end