1function initvalf(fname_)
2% function initvalf(fname_)
3%
4% Reads an initial path from the 'fname_' file for exogenous and endogenous variables
5%
6% INPUTS
7%    fname_:         name of the function or file containing the data
8%
9% OUTPUTS
10%    none
11%
12% SPECIAL REQUIREMENTS
13%    All variables local to this function have an underscore appended to
14%    their name, to minimize clashes with model variables loaded by this function.
15
16% Copyright (C) 2003-2018 Dynare Team
17%
18% This file is part of Dynare.
19%
20% Dynare is free software: you can redistribute it and/or modify
21% it under the terms of the GNU General Public License as published by
22% the Free Software Foundation, either version 3 of the License, or
23% (at your option) any later version.
24%
25% Dynare is distributed in the hope that it will be useful,
26% but WITHOUT ANY WARRANTY; without even the implied warranty of
27% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28% GNU General Public License for more details.
29%
30% You should have received a copy of the GNU General Public License
31% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
32
33global M_ oo_ options_
34
35series_ = 1;
36
37[directory,basename,extension] = fileparts(fname_);
38
39% Auto-detect extension if not provided
40if isempty(extension)
41    if exist([basename '.m'],'file')
42        extension = '.m';
43    elseif exist([basename '.mat'],'file')
44        extension = '.mat';
45    elseif exist([basename '.xls'],'file')
46        extension = '.xls';
47    elseif exist([basename '.xlsx'],'file')
48        extension = '.xlsx';
49    else
50        error(['Can''t find datafile: ' basename '.{m,mat,xls,xlsx}']);
51    end
52end
53
54fullname = [basename extension];
55
56if ~exist(fullname)
57    error(['Can''t find datafile: ' fullname ]);
58end
59
60switch (extension)
61  case '.m'
62    eval(basename);
63  case '.mat'
64    load(basename);
65  case { '.xls', '.xlsx' }
66    [data_,names_v_]=xlsread(fullname); % Octave needs the extension explicitly
67    series_=0;
68  otherwise
69    error(['Unsupported extension for datafile: ' extension])
70end
71
72options_.initval_file = true;
73oo_.endo_simul = [];
74oo_.exo_simul = [];
75
76for i_=1:length(M_.endo_names)
77    if series_ == 1
78        x_ = eval(M_.endo_names{i_});
79        if size(x_,2)>size(x_,1) %oo_.endo_simul must be collection of row vectors
80            oo_.endo_simul = [oo_.endo_simul; x_];
81        else %transpose if column vector
82            oo_.endo_simul = [oo_.endo_simul; x_'];
83        end
84    else
85        k_ = strmatch(M_.endo_names{i_}, names_v_, 'exact');
86        if isempty(k_)
87            error(['INITVAL_FILE: ' M_.endo_names{i_} ' not found'])
88        end
89        x_ = data_(:,k_);
90        oo_.endo_simul = [oo_.endo_simul; x_'];
91    end
92end
93
94for i_=1:length(M_.exo_names)
95    if series_ == 1
96        x_ = eval(M_.exo_names{i_});
97        if size(x_,2)>size(x_,1) %oo_.endo_simul must be collection of row vectors
98            oo_.exo_simul = [oo_.exo_simul x_'];
99        else %if column vector
100            oo_.exo_simul = [oo_.exo_simul x_];
101        end
102    else
103        k_ = strmatch(M_.exo_names{i_}, names_v_, 'exact');
104        if isempty(k_)
105            error(['INITVAL_FILE: ' M_.exo_names{i_} ' not found'])
106        end
107        x_ = data_(:,k_);
108        oo_.exo_simul = [oo_.exo_simul x_];
109    end
110end
111