Added new routines to load data from m, mat or csv files. Used by dynSeries class.
parent
f01b97da18
commit
7e47128212
|
@ -1,4 +1,4 @@
|
|||
function ts = dynSeries(a,b,c,d)
|
||||
function ts = dynSeries(varargin)% dynSeries(a,b,c,d)
|
||||
|
||||
%@info:
|
||||
%! @deftypefn {Function File} {@var{ts} =} dynSeries (@var{a},@var{b},@var{c},@var{d})
|
||||
|
@ -90,16 +90,38 @@ ts = struct;
|
|||
ts.data = [];
|
||||
ts.nobs = 0;
|
||||
ts.vobs = 0;
|
||||
ts.name = [];
|
||||
ts.tex = [];
|
||||
ts.name = {};
|
||||
ts.tex = {};
|
||||
ts.freq = [];
|
||||
ts.Time = dynTime();
|
||||
ts.init = dynDate();%ts.Time = dynTime();
|
||||
|
||||
ts = class(ts,'dynSeries');
|
||||
|
||||
switch nargin
|
||||
case 0
|
||||
% Create an empty dynSeries object.
|
||||
return
|
||||
case 1
|
||||
if isa(varargin{1},'dynDate')
|
||||
if isempty(varargin{1})
|
||||
error(['dynSeries:: ' inputname(1) ' (identified as a dynDate object) must be non empty!'])
|
||||
else
|
||||
% Create an empty dynSeries object with an initial date.
|
||||
ts.init = varargin{1};
|
||||
ts.freq = varargin{1}.freq;
|
||||
end
|
||||
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});
|
||||
elseif check_file_extension(varargin{1},'mat')
|
||||
[freq,init,data,varlist] = load_mat_file_data(varargin{1});
|
||||
elseif check_file_extension(varargin{1},'csv')
|
||||
[freq,init,data,varlist] = load_csv_file_data(varargin{1});
|
||||
else
|
||||
error(['dynSeries:: I''m not able to load data from ' inputname(1) '!'])
|
||||
end
|
||||
end
|
||||
case {2,4}
|
||||
if nargin==2
|
||||
c = [];
|
||||
|
|
|
@ -1,4 +1,42 @@
|
|||
function [list_of_variables, data, time] = readcsv(file, withtime, withnames, noemptycell)
|
||||
function [freq, init, data, varlist] = load_csv_file_data(file, withtime, withnames, noemptycell)
|
||||
|
||||
%@info:
|
||||
%! @deftypefn {Function File} {@var{freq}, @var{init}, @var{data}, @var{varlist} =} load_m_file_data (@var{file}, @var{withtime}, @var{withnames}, @var{noemptycell})
|
||||
%! @anchor{load_csv_file_data}
|
||||
%! @sp 1
|
||||
%! Loads data in a csv file.
|
||||
%! @sp 2
|
||||
%! @strong{Inputs}
|
||||
%! @sp 1
|
||||
%! @table @ @var
|
||||
%! @item file
|
||||
%! string, name of the m file (matlab/octave script).
|
||||
%! @item withtime
|
||||
%! Scalar integer, non zero iff the first column is for the dates of the observations.
|
||||
%! @item withnames
|
||||
%! Scalar integer, non zero iff the first row is for the names of the variables.
|
||||
%! @item noemptycell
|
||||
%! Scalar integer, non zero iff the csv file does not have empty cells.
|
||||
%! @end table
|
||||
%! @sp 2
|
||||
%! @strong{Outputs}
|
||||
%! @sp 1
|
||||
%! @table @ @var
|
||||
%! @item freq
|
||||
%! Scalar integer (1, 4, 12, 52).
|
||||
%! @item init
|
||||
%! dynDate object, initial date.
|
||||
%! @item data
|
||||
%! Matrix of doubles, data.
|
||||
%! @item varlist
|
||||
%! Cell of strings (names of the variables in the database).
|
||||
%! @end table
|
||||
%! @sp 2
|
||||
%! @strong{Remarks}
|
||||
%! @sp 1
|
||||
%! The frequency and initial date can be specified with variables FREQ__ and INIT__ in the matlab/octave script. FREQ__ must be a scalar integer and INIT__ a string like '1938M11', '1945Q3', '1973W3' or '2009'. If these variables are not specified default values for freq and init are 1 and dynDate(1).
|
||||
%! @end deftypefn
|
||||
%@eod:
|
||||
|
||||
% Copyright (C) 2012 Dynare Team
|
||||
%
|
||||
|
@ -19,6 +57,20 @@ function [list_of_variables, data, time] = readcsv(file, withtime, withnames, no
|
|||
|
||||
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
|
||||
|
||||
% Set defaults.
|
||||
if nargin<4
|
||||
noemptycell = 1;
|
||||
if nargin<3
|
||||
withnames = 1;
|
||||
if nargin <2
|
||||
withtime = 1;
|
||||
if nargin<1
|
||||
error('load_csv_file_data:: I need at least one input (name of the csv file)!')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if ~withtime && ~withname && noemptycell
|
||||
% Use matlab builtin routine!
|
||||
data = csvread(file);
|
||||
|
@ -33,7 +85,7 @@ if ~( isequal(withnames,0) || isequal(withnames,1) )
|
|||
end
|
||||
|
||||
% Output initialization
|
||||
time = []; data = []; list_of_variables = [];
|
||||
time = []; data = []; varlist = [];
|
||||
|
||||
% Check if file exists.
|
||||
if check_file_extension(file,'csv')
|
||||
|
@ -83,10 +135,10 @@ if withnames
|
|||
B = B(2:end);
|
||||
C = C(2:end);
|
||||
end
|
||||
list_of_variables = cell(length(B),1);
|
||||
number_of_variables = length(list_of_variables);
|
||||
varlist = cell(length(B),1);
|
||||
number_of_variables = length(varlist);
|
||||
for i=1:number_of_variables
|
||||
list_of_variables(i) = {linee(B(i):C(i))};
|
||||
varlist(i) = {linee(B(i):C(i))};
|
||||
end
|
||||
linea = linea+1;
|
||||
end
|
||||
|
@ -107,10 +159,12 @@ if withtime
|
|||
% Remove the Y (gpm/iris date format) if necessary
|
||||
tmp = { tmp(1:end-1) };
|
||||
end
|
||||
initial_date = dynDate(tmp);
|
||||
init = dynDate(tmp);
|
||||
freq = init.freq;
|
||||
first = 2;
|
||||
else
|
||||
initial_date = dynDate(1);
|
||||
init = dynDate(1);
|
||||
freq = 1;
|
||||
first = 1;
|
||||
end
|
||||
|
|
@ -0,0 +1,124 @@
|
|||
function [freq,init,data,varlist] = load_m_file_data(file)
|
||||
|
||||
%@info:
|
||||
%! @deftypefn {Function File} {@var{freq}, @var{init}, @var{data}, @var{varlist} =} load_m_file_data (@var{file})
|
||||
%! @anchor{load_m_file_data}
|
||||
%! @sp 1
|
||||
%! Loads data in a matlab/octave m-file.
|
||||
%! @sp 2
|
||||
%! @strong{Inputs}
|
||||
%! @sp 1
|
||||
%! @table @ @var
|
||||
%! @item file
|
||||
%! string, name of the m file (matlab/octave script).
|
||||
%! @end table
|
||||
%! @sp 2
|
||||
%! @strong{Outputs}
|
||||
%! @sp 1
|
||||
%! @table @ @var
|
||||
%! @item freq
|
||||
%! Scalar integer (1, 4, 12, 52).
|
||||
%! @item init
|
||||
%! dynDate object, initial date.
|
||||
%! @item data
|
||||
%! Matrix of doubles, data.
|
||||
%! @item varlist
|
||||
%! Cell of strings (names of the variables in the database).
|
||||
%! @end table
|
||||
%! @sp 2
|
||||
%! @strong{Remarks}
|
||||
%! @sp 1
|
||||
%! The frequency and initial date can be specified with variables FREQ__ and INIT__ in the matlab/octave script. FREQ__ must be a scalar integer and INIT__ a string like '1938M11', '1945Q3', '1973W3' or '2009'. If these variables are not specified default values for freq and init are 1 and dynDate(1).
|
||||
%! @end deftypefn
|
||||
%@eod:
|
||||
|
||||
|
||||
% Copyright (C) 2012 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/>.
|
||||
|
||||
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
|
||||
|
||||
run(file);
|
||||
|
||||
if exist('INIT__','var')
|
||||
init = dynDate(INIT__);
|
||||
clear('INIT__')
|
||||
else
|
||||
init = dynDate(1);
|
||||
end
|
||||
|
||||
if exist('FREQ__','var')
|
||||
freq = FREQ__;
|
||||
clear('FREQ__');
|
||||
else
|
||||
freq = 1;
|
||||
end
|
||||
|
||||
list_of_variables = whos();
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
%@test:1
|
||||
%$ % Create a data m-file
|
||||
%$ fid = fopen('data_m_file.m','w');
|
||||
%$ fprintf(fid,'FREQ__ = 4;');
|
||||
%$ fprintf(fid,'INIT__ = ''1938Q4'';');
|
||||
%$ fprintf(fid,'azert = [1; 2; 3; 4; 5];');
|
||||
%$ fprintf(fid,'yuiop = [2; 3; 4; 5; 6];');
|
||||
%$ fclose(fid);
|
||||
%$
|
||||
%$ % Try to read the data m-file
|
||||
%$ try
|
||||
%$ datafile = 'data_m_file';
|
||||
%$ [freq,init,data,varlist] = load_m_file_data(datafile);
|
||||
%$ t(1) = 1;
|
||||
%$ catch exception
|
||||
%$ t(1) = 0;
|
||||
%$ T = all(t);
|
||||
%$ LOG = getReport(exception,'extended');
|
||||
%$ return
|
||||
%$ end
|
||||
%$
|
||||
%$ % Check the results.
|
||||
%$ t(2) = dyn_assert(freq,4);
|
||||
%$ 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 = all(t);
|
||||
%@eof:1
|
|
@ -0,0 +1,111 @@
|
|||
function [freq,init,data,varlist] = load_mat_file_data(file)
|
||||
|
||||
%@info:
|
||||
%! @deftypefn {Function File} {@var{freq}, @var{init}, @var{data}, @var{varlist} =} load_m_file_data (@var{file})
|
||||
%! @anchor{load_m_file_data}
|
||||
%! @sp 1
|
||||
%! Loads data in a matlab/octave mat-file.
|
||||
%! @sp 2
|
||||
%! @strong{Inputs}
|
||||
%! @sp 1
|
||||
%! @table @ @var
|
||||
%! @item file
|
||||
%! string, name of the mat file.
|
||||
%! @end table
|
||||
%! @sp 2
|
||||
%! @strong{Outputs}
|
||||
%! @sp 1
|
||||
%! @table @ @var
|
||||
%! @item freq
|
||||
%! Scalar integer (1, 4, 12, 52).
|
||||
%! @item init
|
||||
%! dynDate object, initial date.
|
||||
%! @item data
|
||||
%! Matrix of doubles, data.
|
||||
%! @item varlist
|
||||
%! Cell of strings (names of the variables in the database).
|
||||
%! @end table
|
||||
%! @sp 2
|
||||
%! @strong{Remarks}
|
||||
%! @sp 1
|
||||
%! The frequency and initial date can be specified with variables FREQ__ and INIT__ in the matlab/octave mat file. FREQ__ must be a scalar integer and INIT__ a string like '1938M11', '1945Q3', '1973W3' or '2009'. If these variables are not specified, default values for freq and init are 1 and dynDate(1).
|
||||
%! @end deftypefn
|
||||
%@eod:
|
||||
|
||||
|
||||
% Copyright (C) 2012 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/>.
|
||||
|
||||
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
|
||||
|
||||
datafile = load(file);
|
||||
|
||||
if isfield(datafile,'INIT__')
|
||||
init = dynDate(datafile.INIT__);
|
||||
datafile = rmfield(datafile, 'INIT__');
|
||||
else
|
||||
init = dynDate(1);
|
||||
end
|
||||
|
||||
if isfield(datafile,'FREQ__')
|
||||
freq = datafile.FREQ__;
|
||||
datafile = rmfield(datafile, 'FREQ__');
|
||||
else
|
||||
freq = 1;
|
||||
end
|
||||
|
||||
data = [];
|
||||
varlist = fieldnames(datafile);
|
||||
|
||||
for i=1:length(varlist)
|
||||
try
|
||||
data = [data, getfield(datafile,varlist{i})];
|
||||
catch
|
||||
error(['load_mat_file:: All the vectors (variables) in ' inputname(1) ' must have the same number of rows (observations)!'])
|
||||
end
|
||||
end
|
||||
|
||||
%@test:1
|
||||
%$ % Create a data mat-file
|
||||
%$ FREQ__ = 12;
|
||||
%$ INIT__ = '1938M11';
|
||||
%$ hagop = [1; 2; 3; 4; 5];
|
||||
%$ bedros = [2; 3; 4; 5; 6];
|
||||
%$ save('datafile_for_test');
|
||||
%$
|
||||
%$ % Try to read the data mat-file
|
||||
%$ t = zeros(8,1);
|
||||
%$ try
|
||||
%$ [freq,init,data,varlist] = load_mat_file_data('datafile_for_test');
|
||||
%$ t(1) = 1;
|
||||
%$ catch exception
|
||||
%$ t = t(1);
|
||||
%$ T = all(t);
|
||||
%$ LOG = getReport(exception,'extended');
|
||||
%$ return
|
||||
%$ end
|
||||
%$
|
||||
%$ % Check the results.
|
||||
%$ t(2) = dyn_assert(freq,12);
|
||||
%$ t(3) = dyn_assert(isa(init,'dynDate'),1);
|
||||
%$ 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 = all(t);
|
||||
%@eof:1
|
Loading…
Reference in New Issue