2013-09-05 11:34:19 +02:00
|
|
|
function save(A,basename,format) % --*-- Unitary tests --*--
|
|
|
|
|
2013-10-18 17:38:43 +02:00
|
|
|
% Saves a dseries object on disk.
|
2013-06-12 16:42:09 +02:00
|
|
|
|
|
|
|
% 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/>.
|
|
|
|
|
2012-12-07 15:37:04 +01:00
|
|
|
if nargin<3 || isempty(format)
|
|
|
|
format = 'csv';
|
|
|
|
end
|
|
|
|
|
|
|
|
if nargin<2 || isempty(basename)
|
|
|
|
basename = inputname(1);
|
|
|
|
end
|
|
|
|
|
|
|
|
switch format
|
|
|
|
case 'm'
|
2013-06-24 15:23:28 +02:00
|
|
|
if exist([basename, '.m'],'file')
|
2013-11-29 18:39:28 +01:00
|
|
|
copyfile([basename, '.m'],[basename, '.old.m']);
|
2013-06-24 15:23:28 +02:00
|
|
|
end
|
2013-07-01 16:50:02 +02:00
|
|
|
fid = fopen([basename, '.m'],'w');
|
2012-12-07 15:37:04 +01:00
|
|
|
fprintf(fid,'%% File created on %s.\n',datestr(now));
|
|
|
|
fprintf(fid,'\n');
|
2014-06-09 11:41:16 +02:00
|
|
|
fprintf(fid,'FREQ__ = %s;\n',num2str(frequency(A)));
|
2014-07-15 16:48:42 +02:00
|
|
|
fprintf(fid,'INIT__ = ''%s'';\n',date2string(firstdate(A)));
|
2012-12-07 15:37:04 +01:00
|
|
|
fprintf(fid,'\n');
|
2013-03-16 00:29:56 +01:00
|
|
|
fprintf(fid,'NAMES__ = {');
|
2014-07-28 17:48:49 +02:00
|
|
|
for i=1:vobs(A)
|
2013-03-16 00:29:56 +01:00
|
|
|
fprintf(fid,[ '''' A.name{i} '''']);
|
2014-07-28 17:48:49 +02:00
|
|
|
if i<vobs(A)
|
2013-03-16 00:29:56 +01:00
|
|
|
fprintf(fid,'; ');
|
|
|
|
end
|
|
|
|
end
|
|
|
|
fprintf(fid,'};\n');
|
2014-07-15 15:43:29 +02:00
|
|
|
str = 'TEX__ = {';
|
2014-07-28 17:48:49 +02:00
|
|
|
for i=1:vobs(A)-1
|
2014-07-15 15:43:29 +02:00
|
|
|
str = [str, '''%s''; '];
|
2013-03-16 00:29:56 +01:00
|
|
|
end
|
2014-07-15 15:43:29 +02:00
|
|
|
str = [str, '''%s''};'];
|
|
|
|
str = sprintf(str, A.tex{:});
|
|
|
|
pattern = '(\w*)(\\\_)';
|
|
|
|
str = regexprep(str, pattern, '$1\\\\_');
|
|
|
|
fprintf(fid,str);
|
|
|
|
fprintf(fid,'\n\n');
|
2014-07-28 17:48:49 +02:00
|
|
|
for v=1:vobs(A)
|
2012-12-07 15:37:04 +01:00
|
|
|
fprintf(fid,'%s = [\n', A.name{v});
|
|
|
|
fprintf(fid,'%15.8g\n',A.data(1:end-1,v));
|
|
|
|
fprintf(fid,'%15.8g];\n\n',A.data(end,v));
|
|
|
|
end
|
|
|
|
fclose(fid);
|
|
|
|
case 'mat'
|
2014-06-09 11:41:16 +02:00
|
|
|
FREQ__ = frequency(A);
|
|
|
|
INIT__ = date2string(firstdate(A));
|
2013-03-16 00:29:56 +01:00
|
|
|
NAMES__ = A.name;
|
|
|
|
TEX__ = A.tex;
|
2012-12-07 15:37:04 +01:00
|
|
|
str = [];
|
2014-07-28 17:48:49 +02:00
|
|
|
for v=1:vobs(A)
|
2012-12-07 15:37:04 +01:00
|
|
|
str = [str, A.name{v} ' = A.data(:,' num2str(v) ');' ];
|
|
|
|
end
|
|
|
|
eval(str);
|
2013-06-24 15:23:28 +02:00
|
|
|
if exist([basename, '.mat'],'file')
|
2013-11-29 18:39:28 +01:00
|
|
|
copyfile([basename, '.mat'],[basename, '.old.mat']);
|
2013-06-24 15:23:28 +02:00
|
|
|
end
|
2013-07-01 16:50:02 +02:00
|
|
|
save([basename '.mat'],'INIT__','FREQ__','NAMES__','TEX__',A.name{:});
|
2012-12-07 15:37:04 +01:00
|
|
|
case 'csv'
|
2013-06-24 15:23:28 +02:00
|
|
|
if exist([basename, '.csv'],'file')
|
2013-11-29 18:39:28 +01:00
|
|
|
copyfile([basename, '.csv'],[basename, '.old.csv']);
|
2013-06-24 15:23:28 +02:00
|
|
|
end
|
2013-07-01 16:50:02 +02:00
|
|
|
fid = fopen([basename, '.csv'],'w');
|
2013-06-27 10:38:49 +02:00
|
|
|
fprintf(fid,',%s', A.name{:});
|
2012-12-07 15:37:04 +01:00
|
|
|
fprintf(fid,'\n');
|
2014-07-28 17:48:49 +02:00
|
|
|
for t=1:nobs(A)
|
2012-12-07 15:37:04 +01:00
|
|
|
str = sprintf(', %15.8g',A.data(t,:));
|
2013-11-06 17:41:11 +01:00
|
|
|
fprintf(fid, '%s%s\n',date2string(A.dates(t)),str);
|
2012-12-07 15:37:04 +01:00
|
|
|
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
|
2013-10-18 17:38:43 +02:00
|
|
|
%$ ts1 = dseries(A,[],A_name,[]);
|
2012-12-07 15:37:04 +01:00
|
|
|
%$ save(ts1,[],'csv');
|
|
|
|
%$ t = 1;
|
|
|
|
%$ catch
|
|
|
|
%$ t = 0;
|
|
|
|
%$ end
|
|
|
|
%$
|
|
|
|
%$ 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
|
2013-10-18 17:38:43 +02:00
|
|
|
%$ ts1 = dseries(A,[],A_name,[]);
|
2012-12-07 15:37:04 +01:00
|
|
|
%$ save(ts1,[],'m');
|
|
|
|
%$ t = 1;
|
|
|
|
%$ catch
|
|
|
|
%$ t = 0;
|
|
|
|
%$ end
|
|
|
|
%$
|
|
|
|
%$ 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
|
2013-10-18 17:38:43 +02:00
|
|
|
%$ ts1 = dseries(A,[],A_name,[]);
|
2013-03-16 00:29:56 +01:00
|
|
|
%$ save(ts1,[],'mat');
|
2012-12-07 15:37:04 +01:00
|
|
|
%$ t = 1;
|
|
|
|
%$ catch
|
|
|
|
%$ t = 0;
|
|
|
|
%$ end
|
|
|
|
%$
|
|
|
|
%$ T = all(t);
|
|
|
|
%@eof:3
|
2013-03-16 00:29:56 +01:00
|
|
|
|
|
|
|
%@test:4
|
|
|
|
%$ % Define a data set.
|
|
|
|
%$ A = [transpose(1:10),2*transpose(1:10)];
|
|
|
|
%$
|
|
|
|
%$ % Define names
|
|
|
|
%$ A_name = {'A1';'A2'};
|
|
|
|
%$
|
|
|
|
%$ % Instantiate a time series object.
|
|
|
|
%$ try
|
2013-10-18 17:38:43 +02:00
|
|
|
%$ ts1 = dseries(A,[],A_name,[]);
|
2013-11-29 18:39:28 +01:00
|
|
|
%$ if isoctave
|
|
|
|
%$ ts1.save('A');
|
|
|
|
%$ else
|
|
|
|
%$ ts1.save;
|
|
|
|
%$ end
|
2013-03-16 00:29:56 +01:00
|
|
|
%$ t = 1;
|
|
|
|
%$ catch
|
|
|
|
%$ t = 0;
|
|
|
|
%$ end
|
|
|
|
%$
|
|
|
|
%$ T = all(t);
|
2013-06-12 16:42:09 +02:00
|
|
|
%@eof:4
|