Commit 2f9e34c5 authored by Sébastien Villemot's avatar Sébastien Villemot
Browse files

Various fixes related to data-loading

- fix support for filenames with extension (.m, .mat, .xls), since the manual
  and the bison grammar both support it
- fix support for XLS files under Octave
- add support for XLSX files
- run test for loading XLS files
parent c88a8aaa
...@@ -1993,9 +1993,10 @@ variable, the file must contain a row vector of the same name. ...@@ -1993,9 +1993,10 @@ variable, the file must contain a row vector of the same name.
MAT-file (extension @file{.mat}): same as for M-files. MAT-file (extension @file{.mat}): same as for M-files.
@item @item
Excel file (extension @file{.xls}): for each endogenous and exogenous, Excel file (extension @file{.xls} or @file{.xlsx}): for each
the file must contain a column of the same name (supported under Octave endogenous and exogenous, the file must contain a column of the same
if the @uref{http://octave.sourceforge.net/io/,io} and name (supported under Octave if the
@uref{http://octave.sourceforge.net/io/,io} and
@uref{http://octave.sourceforge.net/java/,java} packages from @uref{http://octave.sourceforge.net/java/,java} packages from
Octave-Forge are installed, along with a Octave-Forge are installed, along with a
@uref{http://www.java.com/download,Java Runtime Environment}). @uref{http://www.java.com/download,Java Runtime Environment}).
...@@ -3891,7 +3892,7 @@ graphs of smoothed shocks, smoothed observation errors, smoothed and historical ...@@ -3891,7 +3892,7 @@ graphs of smoothed shocks, smoothed observation errors, smoothed and historical
@item datafile = @var{FILENAME} @item datafile = @var{FILENAME}
@anchor{datafile} @anchor{datafile}
The datafile: a @file{.m} file, a @file{.mat} file or, a The datafile: a @file{.m} file, a @file{.mat} file or, a
@file{.xls} file (the latter format is supported under Octave @file{.xls}/@file{.xlsx} file (the latter format is supported under Octave
if the @uref{http://octave.sourceforge.net/io/,io} and if the @uref{http://octave.sourceforge.net/io/,io} and
@uref{http://octave.sourceforge.net/java/,java} packages from @uref{http://octave.sourceforge.net/java/,java} packages from
Octave-Forge are installed, along with a Octave-Forge are installed, along with a
......
...@@ -227,7 +227,7 @@ options_.particle = particle; ...@@ -227,7 +227,7 @@ options_.particle = particle;
options_.TeX = 0; options_.TeX = 0;
% Exel % Exel
options_.xls_sheet = ''; options_.xls_sheet = 1; % Octave does not support the empty string, rather use first sheet
options_.xls_range = ''; options_.xls_range = '';
% Prior draws % Prior draws
......
...@@ -13,7 +13,7 @@ function initvalf(fname_) ...@@ -13,7 +13,7 @@ function initvalf(fname_)
% All variables local to this function have an underscore appended to % All variables local to this function have an underscore appended to
% their name, to minimize clashes with model variables loaded by this function. % their name, to minimize clashes with model variables loaded by this function.
% Copyright (C) 2003-2010 Dynare Team % Copyright (C) 2003-2012 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
...@@ -33,13 +33,39 @@ function initvalf(fname_) ...@@ -33,13 +33,39 @@ function initvalf(fname_)
global M_ oo_ options_ global M_ oo_ options_
series_ = 1; series_ = 1;
if exist(fname_) == 2
eval(fname_); [directory,basename,extension] = fileparts(fname_);
elseif exist([fname_ '.xls']) == 2
[data_,names_v_]=xlsread([fname_ '.xls']); % Auto-detect extension if not provided
series_ = 0; if isempty(extension)
elseif exist([fname_ '.mat']) == 2 if exist([basename '.m'],'file')
load(fname_); extension = '.m';
elseif exist([basename '.mat'],'file')
extension = '.mat';
elseif exist([basename '.xls'],'file')
extension = '.xls';
elseif exist([basename '.xlsx'],'file')
extension = '.xlsx';
else
error(['Can''t find datafile: ' basename '.{m,mat,xls,xlsx}']);
end
end
fullname = [basename extension];
if ~exist(fullname)
error(['Can''t find datafile: ' fullname ]);
end
switch (extension)
case '.m'
eval(basename);
case '.mat'
load(basename);
case { '.xls', '.xlsx' }
[data_,names_v_]=xlsread(fullname); % Octave needs the extension explicitly
otherwise
error(['Unsupported extension for datafile: ' extension])
end end
options_.initval_file = 1; options_.initval_file = 1;
......
...@@ -17,7 +17,7 @@ function dyn_data_01=read_variables(file_name_01,var_names_01,dyn_data_01,xls_sh ...@@ -17,7 +17,7 @@ function dyn_data_01=read_variables(file_name_01,var_names_01,dyn_data_01,xls_sh
% all local variables have complicated names in order to avoid name % all local variables have complicated names in order to avoid name
% conflicts with possible user variable names % conflicts with possible user variable names
% Copyright (C) 2005-2011 Dynare Team % Copyright (C) 2005-2012 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
...@@ -36,53 +36,72 @@ function dyn_data_01=read_variables(file_name_01,var_names_01,dyn_data_01,xls_sh ...@@ -36,53 +36,72 @@ function dyn_data_01=read_variables(file_name_01,var_names_01,dyn_data_01,xls_sh
old_pwd = pwd; old_pwd = pwd;
[path_name_02,file_name_02,ext_name_02] = fileparts(file_name_01); [directory,basename,extension] = fileparts(file_name_01);
if ~isempty(path_name_02) if ~isempty(directory)
file_name_01 = [file_name_02, ext_name_02]; cd(directory)
cd(path_name_02)
end end
dyn_size_01 = size(dyn_data_01,1); dyn_size_01 = size(dyn_data_01,1);
var_size_01 = size(var_names_01,1); var_size_01 = size(var_names_01,1);
if exist([file_name_01 '.m'],'file')
file_name_02 = [file_name_01 '.m']; % Auto-detect extension if not provided
dyn_instr_01 = file_name_01; if isempty(extension)
eval(dyn_instr_01); if exist([basename '.m'],'file')
for dyn_i_01=1:var_size_01 extension = '.m';
dyn_tmp_01 = eval(var_names_01(dyn_i_01,:)); elseif exist([basename '.mat'],'file')
if length(dyn_tmp_01) > dyn_size_01 && dyn_size_01 > 0 extension = '.mat';
cd(old_pwd) elseif exist([basename '.xls'],'file')
error('data size is too large') extension = '.xls';
end elseif exist([basename '.xlsx'],'file')
dyn_data_01(:,dyn_i_01) = dyn_tmp_01; extension = '.xlsx';
else
error(['Can''t find datafile: ' basename '.{m,mat,xls,xlsx}']);
end end
elseif exist([file_name_01 '.mat'],'file') end
file_name_02 = [file_name_01 '.mat'];
s = load(file_name_01); fullname = [basename extension];
for dyn_i_01=1:var_size_01
dyn_tmp_01 = s.(deblank(var_names_01(dyn_i_01,:))); if ~exist(fullname)
if length(dyn_tmp_01) > dyn_size_01 && dyn_size_01 > 0 error(['Can''t find datafile: ' fullname ]);
cd(old_pwd) end
error('data size is too large')
switch (extension)
case '.m'
eval(basename);
for dyn_i_01=1:var_size_01
dyn_tmp_01 = eval(var_names_01(dyn_i_01,:));
if length(dyn_tmp_01) > dyn_size_01 && dyn_size_01 > 0
cd(old_pwd)
error('data size is too large')
end
dyn_data_01(:,dyn_i_01) = dyn_tmp_01;
end end
dyn_data_01(:,dyn_i_01) = dyn_tmp_01; case '.mat'
end s = load(basename);
elseif exist([file_name_01 '.xls'],'file') for dyn_i_01=1:var_size_01
file_name_02 = [file_name_01 '.xls']; dyn_tmp_01 = s.(deblank(var_names_01(dyn_i_01,:)));
[num,txt,raw] = xlsread(file_name_01,xls_sheet,xls_range); if length(dyn_tmp_01) > dyn_size_01 && dyn_size_01 > 0
for dyn_i_01=1:var_size_01 cd(old_pwd)
iv = strmatch(var_names_01(dyn_i_01,:),raw(1,:),'exact'); error('data size is too large')
dyn_tmp_01 = [raw{2:end,iv}]'; end
if length(dyn_tmp_01) > dyn_size_01 && dyn_size_01 > 0 dyn_data_01(:,dyn_i_01) = dyn_tmp_01;
cd(old_pwd)
error('data size is too large')
end end
dyn_data_01(:,dyn_i_01) = dyn_tmp_01; case { '.xls', '.xlsx' }
end [num,txt,raw] = xlsread(fullname,xls_sheet,xls_range); % Octave needs the extension explicitly
else for dyn_i_01=1:var_size_01
cd(old_pwd) iv = strmatch(var_names_01(dyn_i_01,:),raw(1,:),'exact');
error(['Can''t find datafile: ' file_name_01 ]); dyn_tmp_01 = [raw{2:end,iv}]';
if length(dyn_tmp_01) > dyn_size_01 && dyn_size_01 > 0
cd(old_pwd)
error('data size is too large')
end
dyn_data_01(:,dyn_i_01) = dyn_tmp_01;
end
otherwise
cd(old_pwd)
error(['Unsupported extension for datafile: ' extension])
end end
cd(old_pwd) cd(old_pwd)
disp(sprintf('Loading %d observations from %s\n',... disp(sprintf('Loading %d observations from %s\n',...
size(dyn_data_01,1),file_name_02)) size(dyn_data_01,1),fullname))
...@@ -60,6 +60,7 @@ MODFILES = \ ...@@ -60,6 +60,7 @@ MODFILES = \
arima/mod2a.mod \ arima/mod2a.mod \
arima/mod2b.mod \ arima/mod2b.mod \
arima/mod2c.mod \ arima/mod2c.mod \
data/mod1a.mod \
fs2000/fs2000.mod \ fs2000/fs2000.mod \
fs2000/fs2000a.mod \ fs2000/fs2000a.mod \
fs2000/fs2000c.mod \ fs2000/fs2000c.mod \
...@@ -168,6 +169,7 @@ EXTRA_DIST = \ ...@@ -168,6 +169,7 @@ EXTRA_DIST = \
external_function/extFunWithFirstAndSecondDerivs.m \ external_function/extFunWithFirstAndSecondDerivs.m \
expectations/expectation_ss_old_steadystate.m \ expectations/expectation_ss_old_steadystate.m \
steady_state/walsh1_old_ss_steadystate.m \ steady_state/walsh1_old_ss_steadystate.m \
data/test.xls \
fs2000/fs2000a_steadystate.m \ fs2000/fs2000a_steadystate.m \
fs2000/fsdat_simul.m \ fs2000/fsdat_simul.m \
k_order_perturbation/run_fs2000kplusplus.m \ k_order_perturbation/run_fs2000kplusplus.m \
......
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