File: load_xls_file_data.m

package info (click to toggle)
dynare 4.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,356 kB
  • ctags: 15,842
  • sloc: cpp: 77,029; ansic: 29,056; pascal: 13,241; sh: 4,811; objc: 3,061; yacc: 3,013; makefile: 1,479; lex: 1,258; python: 162; lisp: 54; xml: 8
file content (138 lines) | stat: -rw-r--r-- 4,154 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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       dates 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.
if isoctave && ~user_has_octave_forge_package('io')
    error('The io package is required to read XLS/XLSX files from Octave')
end
[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 && n2~=t2  %only one column present, but no var name in header text
    % The file contains no informations about the dates.
    notime = 0;
    noname = 1;
elseif isequal(t2,1) && t1>=t2 && n2==t2 %only one column present with var name in header text
    % The file contains no informations about the variables.
    notime = 1;
    noname = 0;
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 = dates(1,1);
varlist = [];
data = num;

% Update freq.
if ~notime
    if isempty(txt{1,1})
        first_date = txt{2,1};
    else
        first_date = txt{1,1};
    end
    if isnumeric(first_date) && isint(first_date)
        first_date = [num2str(first_date) 'Y'];
    end
    if isdate(first_date)
        init = dates(first_date);
        freq = init.freq;
    else
        error('load_xls_file_data: I am not able to read the dates!')
    end
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