Commit 865f3000 authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Make load and save routines for dynSeries objects support tex variable names.

parent 9a9f8ebf
......@@ -106,11 +106,12 @@ switch nargin
elseif ischar(varargin{1})
% Create a dynSeries object loading data in a file (*.csv, *.m, *.mat).
if check_file_extension(varargin{1},'m')
[freq,init,data,varlist] = load_m_file_data(varargin{1});
[freq,init,data,varlist,tex] = load_m_file_data(varargin{1});
elseif check_file_extension(varargin{1},'mat')
[freq,init,data,varlist] = load_mat_file_data(varargin{1});
[freq,init,data,varlist,tex] = load_mat_file_data(varargin{1});
elseif check_file_extension(varargin{1},'csv')
[freq,init,data,varlist] = load_csv_file_data(varargin{1});
tex = [];
else
error(['dynSeries:: I''m not able to load data from ' inputname(1) '!'])
end
......@@ -120,9 +121,13 @@ switch nargin
ts.name = varlist;
ts.vobs = length(varlist);
ts.nobs = size(data,1);
ts.tex = cell(ts.vobs,1);
for i=1:ts.vobs
ts.tex(i) = {name2tex(varlist{i})};
if isempty(tex)
ts.tex = cell(ts.vobs,1);
for i=1:ts.vobs
ts.tex(i) = {name2tex(varlist{i})};
end
else
ts.tex = tex;
end
end
case {2,3,4}
......
......@@ -17,6 +17,22 @@ switch format
fprintf(fid,'FREQ__ = %s;\n',num2str(A.freq));
fprintf(fid,'INIT__ = '' %s'';\n',A.init.format);
fprintf(fid,'\n');
fprintf(fid,'NAMES__ = {');
for i=1:A.vobs
fprintf(fid,[ '''' A.name{i} '''']);
if i<A.vobs
fprintf(fid,'; ');
end
end
fprintf(fid,'};\n');
fprintf(fid,'TEX__ = {');
for i=1:A.vobs
fprintf(fid,['''' A.tex{i} '''']);
if i<A.vobs
fprintf(fid,'; ');
end
end
fprintf(fid,'};\n');
for v=1:A.vobs
fprintf(fid,'%s = [\n', A.name{v});
fprintf(fid,'%15.8g\n',A.data(1:end-1,v));
......@@ -26,12 +42,14 @@ switch format
case 'mat'
FREQ__ = A.freq;
INIT__ = A.init.format;
NAMES__ = A.name;
TEX__ = A.tex;
str = [];
for v=1:A.vobs
str = [str, A.name{v} ' = A.data(:,' num2str(v) ');' ];
end
eval(str);
save([basename '.mat'],'INIT__','FREQ__',A.name{:});
save([basename '.mat'],'INIT__','FREQ__','NAMES__','TEX__',A.name{:});
case 'csv'
fid = fopen([basename, '.csv'],'w');
fprintf(fid,', %s', A.name{:});
......@@ -92,7 +110,7 @@ end
%$ % Instantiate a time series object.
%$ try
%$ ts1 = dynSeries(A,[],A_name,[]);
%$ save(ts1,[],'m');
%$ save(ts1,[],'mat');
%$ t = 1;
%$ catch
%$ t = 0;
......@@ -100,3 +118,22 @@ end
%$
%$ 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'};
%$
%$ % Instantiate a time series object.
%$ try
%$ ts1 = dynSeries(A,[],A_name,[]);
%$ ts1.save;
%$ t = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ T = all(t);
%@eof:4
\ No newline at end of file
function [freq,init,data,varlist] = load_m_file_data(file)
function [freq,init,data,varlist,tex] = load_m_file_data(file)
%@info:
%! @deftypefn {Function File} {@var{freq}, @var{init}, @var{data}, @var{varlist} =} load_m_file_data (@var{file})
......@@ -33,7 +33,7 @@ function [freq,init,data,varlist] = load_m_file_data(file)
%@eod:
% Copyright (C) 2012 Dynare Team
% Copyright (C) 2012, 2013 Dynare Team
%
% This file is part of Dynare.
%
......@@ -50,8 +50,6 @@ function [freq,init,data,varlist] = load_m_file_data(file)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
[basename, ext] = strtok(file,'.');
run(basename);
......@@ -70,27 +68,52 @@ else
freq = 1;
end
list_of_variables = whos();
if exist('NAMES__','var')
varlist0 = NAMES__;
clear('NAMES__');
else
varlist0 = [];
list_of_variables = [];
end
if exist('TEX__','var')
tex = TEX__;
clear('TEX__');
else
tex = [];
end
if isempty(varlist0)
list_of_variables = whos();
end
data = [];
varlist = {};
for i=1:length(list_of_variables)
if isequal(list_of_variables(i).name,'freq') || isequal(list_of_variables(i).name,'time') || isequal(list_of_variables(i).name,'data') || isequal(list_of_variables(i).name,'varlist')
continue
if isempty(varlist0)
for i=1:length(list_of_variables)
if isequal(list_of_variables(i).name,'freq') || isequal(list_of_variables(i).name,'time') || isequal(list_of_variables(i).name,'data') || isequal(list_of_variables(i).name,'varlist')
continue
end
if list_of_variables(i).global || list_of_variables(i).persistent
continue
end
if list_of_variables(i).complex || ~strcmp(list_of_variables(i).class,'double')
continue
end
try
eval(['data = [data, ' list_of_variables(i).name '];'])
eval(['varlist = {varlist{:}, ''' list_of_variables(i).name '''};'])
catch
error(['load_m_file:: All the vectors (variables) in ' inputname(1) ' must have the same number of rows (observations)!'])
end
end
if list_of_variables(i).global || list_of_variables(i).persistent
continue
end
if list_of_variables(i).complex || ~strcmp(list_of_variables(i).class,'double')
continue
end
try
eval(['data = [data, ' list_of_variables(i).name '];'])
eval(['varlist = {varlist{:}, ''' list_of_variables(i).name '''};'])
catch
error(['load_m_file:: All the vectors (variables) in ' inputname(1) ' must have the same number of rows (observations)!'])
else
for i=1:length(varlist0)
eval(['data = [data, ' varlist0{i} '];'])
end
varlist = varlist0;
end
%@test:1
......@@ -98,6 +121,8 @@ end
%$ fid = fopen('data_m_file.m','w');
%$ fprintf(fid,'FREQ__ = 4;');
%$ fprintf(fid,'INIT__ = ''1938Q4'';');
%$ fprintf(fid,'NAMES__ = {''azert'';''yuiop''};');
%$ fprintf(fid,'TEX__ = {''azert'';''yuiop''};');
%$ fprintf(fid,'azert = [1; 2; 3; 4; 5];');
%$ fprintf(fid,'yuiop = [2; 3; 4; 5; 6];');
%$ fclose(fid);
......@@ -105,7 +130,7 @@ end
%$ % Try to read the data m-file
%$ try
%$ datafile = 'data_m_file';
%$ [freq,init,data,varlist] = load_m_file_data(datafile);
%$ [freq,init,data,varlist,tex] = load_m_file_data(datafile);
%$ t(1) = 1;
%$ catch exception
%$ t(1) = 0;
......@@ -119,8 +144,9 @@ end
%$ t(3) = dyn_assert(isa(init,'dynDate'),1);
%$ t(4) = dyn_assert(init.freq,4);
%$ t(5) = dyn_assert(init.time,[1938 4]);
%$ t(6) = dyn_assert(varlist,{'azert','yuiop'});
%$ t(7) = dyn_assert(data(:,1),[1;2;3;4;5]);
%$ t(8) = dyn_assert(data(:,2),[2;3;4;5;6]);
%$ t(6) = dyn_assert(varlist,{'azert';'yuiop'});
%$ t(7) = dyn_assert(tex,{'azert';'yuiop'});
%$ t(8) = dyn_assert(data(:,1),[1;2;3;4;5]);
%$ t(9) = dyn_assert(data(:,2),[2;3;4;5;6]);
%$ T = all(t);
%@eof:1
\ No newline at end of file
function [freq,init,data,varlist] = load_mat_file_data(file)
function [freq,init,data,varlist,tex] = load_mat_file_data(file)
%@info:
%! @deftypefn {Function File} {@var{freq}, @var{init}, @var{data}, @var{varlist} =} load_m_file_data (@var{file})
......@@ -33,7 +33,7 @@ function [freq,init,data,varlist] = load_mat_file_data(file)
%@eod:
% Copyright (C) 2012 Dynare Team
% Copyright (C) 2012, 2013 Dynare Team
%
% This file is part of Dynare.
%
......@@ -49,8 +49,6 @@ function [freq,init,data,varlist] = load_mat_file_data(file)
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
datafile = load(file);
......@@ -68,8 +66,24 @@ else
freq = 1;
end
if isfield(datafile,'NAMES__')
varlist = datafile.NAMES__;
datafile = rmfield(datafile, 'NAMES__');
else
varlist = [];
end
if isfield(datafile,'TEX__')
tex = datafile.TEX__;
datafile = rmfield(datafile, 'TEX__');
else
tex = [];
end
data = [];
varlist = fieldnames(datafile);
if isempty(varlist)
varlist = fieldnames(datafile);
end
for i=1:length(varlist)
try
......@@ -83,6 +97,8 @@ end
%$ % Create a data mat-file
%$ FREQ__ = 12;
%$ INIT__ = '1938M11';
%$ NAMES__ = {'hagop'; 'bedros'};
%$ TEX__ = NAMES__;
%$ hagop = [1; 2; 3; 4; 5];
%$ bedros = [2; 3; 4; 5; 6];
%$ save('datafile_for_test');
......@@ -90,7 +106,7 @@ end
%$ % Try to read the data mat-file
%$ t = zeros(8,1);
%$ try
%$ [freq,init,data,varlist] = load_mat_file_data('datafile_for_test');
%$ [freq,init,data,varlist,tex] = load_mat_file_data('datafile_for_test');
%$ t(1) = 1;
%$ catch exception
%$ t = t(1);
......@@ -105,7 +121,8 @@ end
%$ t(4) = dyn_assert(init.freq,12);
%$ t(5) = dyn_assert(init.time,[1938 11]);
%$ t(6) = dyn_assert(varlist,{'hagop';'bedros'});
%$ t(7) = dyn_assert(data(:,1),[1;2;3;4;5]);
%$ t(8) = dyn_assert(data(:,2),[2;3;4;5;6]);
%$ t(7) = dyn_assert(varlist,{'hagop';'bedros'});
%$ t(8) = dyn_assert(data(:,1),[1;2;3;4;5]);
%$ t(9) = dyn_assert(data(:,2),[2;3;4;5;6]);
%$ T = all(t);
%@eof:1
\ No newline at end of file
FREQ__ = 4;
INIT__ = '1994Q3';
NAMES__ = {'azert';'yuiop'};
TEX__ = {'azert';'yuiop'};
azert = randn(100,1);
yuiop = randn(100,1);
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment