Added the possibility to instantiate the dynSeries class from an excel (xls or xlsx) file.
Closes #348
(cherry picked from commit b7b4a9afdc
)
time-shift
parent
44d64fcfec
commit
109066de2b
|
@ -114,6 +114,19 @@ switch nargin
|
|||
elseif check_file_extension(varargin{1},'csv')
|
||||
[freq,init,data,varlist] = load_csv_file_data(varargin{1});
|
||||
tex = [];
|
||||
elseif check_file_extension(varargin{1},'xls') || check_file_extension(varargin{1},'xlsx')
|
||||
if ~isempty(who('global','options_'));
|
||||
% Check that the object is instantiated within a dynare session so that options_ global structure exists.
|
||||
% Should provide latter a mechanism to pass range and sheet to dynSeries constructor...
|
||||
range = evalin('base','options_.xls_range');
|
||||
sheet = evalin('base','options_.xls_sheet');
|
||||
else
|
||||
% By default only the (whole) first sheet is loaded.
|
||||
range = [];
|
||||
sheet = [];
|
||||
end
|
||||
[freq,init,data,varlist] = load_xls_file_data(varargin{1}, sheet, range);
|
||||
tex = [];
|
||||
else
|
||||
error(['dynSeries:: I''m not able to load data from ' inputname(1) '!'])
|
||||
end
|
||||
|
|
|
@ -0,0 +1,125 @@
|
|||
function [freq, init, data, varlist] = load_xls_file_data(file, sheet, range)
|
||||
|
||||
% Loads data in a xls file.
|
||||
%
|
||||
% INPUTS
|
||||
% o file string, name of the file (with extension).
|
||||
% o sheet string, name of the sheet to be read.
|
||||
% o range string of the form 'B2:D6'
|
||||
%
|
||||
% OUTPUTS
|
||||
% o freq integer scalar (1, 4, 12 or 52), code for frequency.
|
||||
% o init dynDate object, initial date of the sample.
|
||||
% o data matrix of doubles, the raw data.
|
||||
% o varlist cell of strings (column), names of the variables in the database.
|
||||
%
|
||||
% REMARKS
|
||||
% The range argument is only available on windows platform (with Excel installed).
|
||||
|
||||
% 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/>.
|
||||
|
||||
% Set defaults.
|
||||
if nargin<3 || isempty(range)
|
||||
range = '';
|
||||
if nargin<2 || isempty(sheet)
|
||||
sheet = 1;
|
||||
if nargin<1 || isempty(file)
|
||||
error('load_xls_file_data:: I need at least one input (name of the xls or xlsx file)!')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
% Check file extension.
|
||||
if ~(check_file_extension(file,'xls') || check_file_extension(file,'xlsx'))
|
||||
ext = get_file_extension(file);
|
||||
if isempty(ext)
|
||||
if exist([file '.xls'],'file')
|
||||
file = [file '.xls'];
|
||||
elseif exist([file '.xlsx'],'file')
|
||||
file = [file '.xlsx'];
|
||||
else
|
||||
error(['load_xls_file_data:: Unable to find the data file ' file ' with an xls or xlsx extension!'])
|
||||
end
|
||||
else
|
||||
error(['load_xls_file_data:: The data file ' file ' has wrong extension (must be either xls or xlsx)!'])
|
||||
end
|
||||
end
|
||||
|
||||
% load excel file.
|
||||
[num,txt,raw] = xlsread(file, sheet, range);
|
||||
|
||||
% Get dimensions of num, txt and raw
|
||||
[n1, n2] = size(num);
|
||||
[t1, t2] = size(txt);
|
||||
[r1, r2] = size(raw);
|
||||
|
||||
% Check the content of the file.
|
||||
if isequal(t1,0) && isequal(t2,0)
|
||||
% The file contains no informations about the variables and dates.
|
||||
notime = 1;
|
||||
noname = 1;
|
||||
elseif isequal(t2,1) && t1>=t2
|
||||
% The file contains no informations about the variables.
|
||||
notime = 0;
|
||||
noname = 1;
|
||||
elseif isequal(t1,1) && t2>=t1
|
||||
% The file contains no informations about the dates.
|
||||
notime = 1;
|
||||
noname = 0;
|
||||
else
|
||||
% The file contains informations about the variables and dates.
|
||||
notime = 0;
|
||||
noname = 0;
|
||||
end
|
||||
|
||||
% Output initialization.
|
||||
freq = 1;
|
||||
init = dynDate(1);
|
||||
varlist = [];
|
||||
data = num;
|
||||
|
||||
% Update f
|
||||
req.
|
||||
if ~notime
|
||||
if isempty(txt{1,1})
|
||||
first_date = txt{2,1};
|
||||
else
|
||||
first_date = txt{1,1};
|
||||
end
|
||||
init = dynDate(first_date);
|
||||
freq = init.freq;
|
||||
end
|
||||
|
||||
% Update varlist.
|
||||
if ~noname
|
||||
if notime
|
||||
varlist = transpose(txt);
|
||||
else
|
||||
varlist = transpose(txt(1,2:end));
|
||||
end
|
||||
% Remove leading and trailing white spaces
|
||||
for i=1:length(varlist)
|
||||
varlist(i) = {strtrim(varlist{i})};
|
||||
end
|
||||
else
|
||||
% set default names
|
||||
varlist = cell(n2,1);
|
||||
for i=1:n2
|
||||
varlist(i) = {['Variable_' int2str(i)]};
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue