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.
MAT-file (extension @file{.mat}): same as for M-files.
@item
Excel file (extension @file{.xls}): for each endogenous and exogenous,
the file must contain a column of the same name (supported under Octave
if the @uref{http://octave.sourceforge.net/io/,io} and
Excel file (extension @file{.xls} or @file{.xlsx}): for each
endogenous and exogenous, the file must contain a column of the same
name (supported under Octave if the
@uref{http://octave.sourceforge.net/io/,io} and
@uref{http://octave.sourceforge.net/java/,java} packages from
Octave-Forge are installed, along with a
@uref{http://www.java.com/download,Java Runtime Environment}).
......@@ -3891,7 +3892,7 @@ graphs of smoothed shocks, smoothed observation errors, smoothed and historical
@item datafile = @var{FILENAME}
@anchor{datafile}
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
@uref{http://octave.sourceforge.net/java/,java} packages from
Octave-Forge are installed, along with a
......
......@@ -227,7 +227,7 @@ options_.particle = particle;
options_.TeX = 0;
% Exel
options_.xls_sheet = '';
options_.xls_sheet = 1; % Octave does not support the empty string, rather use first sheet
options_.xls_range = '';
% Prior draws
......
......@@ -13,7 +13,7 @@ function initvalf(fname_)
% All variables local to this function have an underscore appended to
% 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.
%
......@@ -33,13 +33,39 @@ function initvalf(fname_)
global M_ oo_ options_
series_ = 1;
if exist(fname_) == 2
eval(fname_);
elseif exist([fname_ '.xls']) == 2
[data_,names_v_]=xlsread([fname_ '.xls']);
series_ = 0;
elseif exist([fname_ '.mat']) == 2
load(fname_);
[directory,basename,extension] = fileparts(fname_);
% Auto-detect extension if not provided
if isempty(extension)
if exist([basename '.m'],'file')
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
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
% all local variables have complicated names in order to avoid name
% conflicts with possible user variable names
% Copyright (C) 2005-2011 Dynare Team
% Copyright (C) 2005-2012 Dynare Team
%
% 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
old_pwd = pwd;
[path_name_02,file_name_02,ext_name_02] = fileparts(file_name_01);
if ~isempty(path_name_02)
file_name_01 = [file_name_02, ext_name_02];
cd(path_name_02)
[directory,basename,extension] = fileparts(file_name_01);
if ~isempty(directory)
cd(directory)
end
dyn_size_01 = size(dyn_data_01,1);
var_size_01 = size(var_names_01,1);
if exist([file_name_01 '.m'],'file')
file_name_02 = [file_name_01 '.m'];
dyn_instr_01 = file_name_01;
eval(dyn_instr_01);
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;
% Auto-detect extension if not provided
if isempty(extension)
if exist([basename '.m'],'file')
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
elseif exist([file_name_01 '.mat'],'file')
file_name_02 = [file_name_01 '.mat'];
s = load(file_name_01);
for dyn_i_01=1:var_size_01
dyn_tmp_01 = s.(deblank(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
fullname = [basename extension];
if ~exist(fullname)
error(['Can''t find datafile: ' fullname ]);
end
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
dyn_data_01(:,dyn_i_01) = dyn_tmp_01;
end
elseif exist([file_name_01 '.xls'],'file')
file_name_02 = [file_name_01 '.xls'];
[num,txt,raw] = xlsread(file_name_01,xls_sheet,xls_range);
for dyn_i_01=1:var_size_01
iv = strmatch(var_names_01(dyn_i_01,:),raw(1,:),'exact');
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')
case '.mat'
s = load(basename);
for dyn_i_01=1:var_size_01
dyn_tmp_01 = s.(deblank(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
dyn_data_01(:,dyn_i_01) = dyn_tmp_01;
end
else
cd(old_pwd)
error(['Can''t find datafile: ' file_name_01 ]);
case { '.xls', '.xlsx' }
[num,txt,raw] = xlsread(fullname,xls_sheet,xls_range); % Octave needs the extension explicitly
for dyn_i_01=1:var_size_01
iv = strmatch(var_names_01(dyn_i_01,:),raw(1,:),'exact');
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
cd(old_pwd)
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 = \
arima/mod2a.mod \
arima/mod2b.mod \
arima/mod2c.mod \
data/mod1a.mod \
fs2000/fs2000.mod \
fs2000/fs2000a.mod \
fs2000/fs2000c.mod \
......@@ -168,6 +169,7 @@ EXTRA_DIST = \
external_function/extFunWithFirstAndSecondDerivs.m \
expectations/expectation_ss_old_steadystate.m \
steady_state/walsh1_old_ss_steadystate.m \
data/test.xls \
fs2000/fs2000a_steadystate.m \
fs2000/fsdat_simul.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