save.m 4.53 KB
Newer Older
1
function save(o, basename, format) % --*-- Unitary tests --*--
2 3 4

% Saves a dseries object on disk.

5
% Copyright (C) 2013-2017 Dynare Team
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
%
% 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/>.

if nargin<3 || isempty(format)
23
    format = 'mat';
24 25 26
end

if nargin<2 || isempty(basename)
27
    basename = 'dynare_series';
28 29 30 31
end

switch format
  case 'm'
Stéphane Adjemian (Charybdis)'s avatar
Stéphane Adjemian (Charybdis) committed
32 33 34
    currentdirectorycontent = dir();
    if ismember([basename, '.m'],{currentdirectorycontent.name})
        copyfile([basename, '.m'],[basename, '.m.csv']);
35 36 37 38
    end
    fid = fopen([basename, '.m'],'w');
    fprintf(fid,'%% File created on %s.\n',datestr(now));
    fprintf(fid,'\n');
39 40
    fprintf(fid,'FREQ__ = %s;\n',num2str(frequency(o)));
    fprintf(fid,'INIT__ = ''%s'';\n',date2string(firstdate(o)));
41 42
    fprintf(fid,'\n');
    fprintf(fid,'NAMES__ = {');
43 44 45
    for i = 1:vobs(o)
        fprintf(fid,[ '''' o.name{i}  '''']);
        if i<vobs(o)
46 47 48 49 50
            fprintf(fid,'; ');
        end
    end
    fprintf(fid,'};\n');
    str = 'TEX__ = {';
51
    for i=1:vobs(o)-1
52 53 54
        str = [str, '''%s''; '];
    end
    str = [str, '''%s''};'];
55
    str = sprintf(str, o.tex{:});
56 57 58
    pattern = '(\w*)(\\\_)';
    str = regexprep(str, pattern, '$1\\\\_');
    fprintf(fid,str);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    fprintf(fid,'\n');
    fprintf(fid,'OPS__ = {');
    for i = 1:vobs(o)
        if isempty(o.ops{i})
            fprintf(fid,[ '[]']);
        else
            fprintf(fid,[ '''' o.ops{i}  '''']);
        end
        if i<vobs(o)
            fprintf(fid,'; ');
        end
    end
    fprintf(fid,'};\n\n');
    for v = 1:vobs(o)
        fprintf(fid,'%s = [\n', o.name{v});
        fprintf(fid,'%15.8g\n', o.data(1:end-1,v));
        fprintf(fid,'%15.8g];\n\n', o.data(end,v));
76 77 78
    end
    fclose(fid);
  case 'mat'
79 80 81 82 83
    FREQ__ = frequency(o);
    INIT__ = date2string(firstdate(o));
    NAMES__ = o.name;
    TEX__ = o.tex;
    OPS__ = o.ops;
84
    str = [];
85 86
    for v = 1:vobs(o)
        str = sprintf('%s %s = o.data(:,%s);', str, o.name{v}, num2str(v));
87 88
    end
    eval(str);
Stéphane Adjemian (Charybdis)'s avatar
Stéphane Adjemian (Charybdis) committed
89
    currentdirectorycontent = dir();
90 91
    if ismember([basename, '.mat'], {currentdirectorycontent.name})
        copyfile([basename, '.mat'], [basename, '.old.mat']);
92
    end
93
    save([basename '.mat'], 'INIT__', 'FREQ__', 'NAMES__', 'TEX__', 'OPS__', o.name{:});
94
  case 'csv'
Stéphane Adjemian (Charybdis)'s avatar
Stéphane Adjemian (Charybdis) committed
95 96
    currentdirectorycontent = dir();
    if ismember([basename, '.csv'],{currentdirectorycontent.name})
97 98 99
        copyfile([basename, '.csv'],[basename, '.old.csv']);
    end
    fid = fopen([basename, '.csv'],'w');
100
    fprintf(fid,',%s', o.name{:});
101
    fprintf(fid,'\n');
102 103 104
    for t = 1:nobs(o)
        str = sprintf(', %15.8g',o.data(t,:));
        fprintf(fid, '%s%s\n',date2string(o.dates(t)), str);
105 106 107 108 109 110 111 112 113 114 115 116 117 118
    end
    fclose(fid);
end

%@test:1
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ try
%$    ts1 = dseries(A,[],A_name,[]);
119
%$    save(ts1,'ts1','csv');
120 121 122 123 124
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
125 126
%$ delete('ts1.csv');
%$
127 128 129 130 131 132 133 134 135 136 137 138 139
%$ T = all(t);
%@eof:1

%@test:2
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ try
%$    ts1 = dseries(A,[],A_name,[]);
140
%$    save(ts1,'ts1','m');
141 142 143 144 145
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
146 147
%$ delete('ts1.m');
%$
148 149 150 151 152 153 154 155 156 157 158 159 160
%$ T = all(t);
%@eof:2

%@test:3
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ try
%$    ts1 = dseries(A,[],A_name,[]);
161
%$    save(ts1,'ts1','mat');
162 163 164 165 166
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
167 168
%$ delete('ts1.mat');
%$
169 170 171 172 173 174 175 176 177 178
%$ T = all(t);
%@eof:3

%@test:4
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
179
%$ % Instantiate and save a time series object.
180 181
%$ try
%$    ts1 = dseries(A,[],A_name,[]);
182
%$    ts1.save;
183 184 185 186 187
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
188
%$ delete('dynare_series.mat');
189
%$
190 191
%$ T = all(t);
%@eof:4