load_m_file_data.m 2.93 KB
Newer Older
1
function [freq, init, data, varlist, tex, ops, tags] = load_m_file_data(file) % --*-- Unitary tests --*--
2
3
4

% Loads data in a matlab/octave script.
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
5
% INPUTS
6
7
%  o file         string, name of the matlab/octave script (with path)
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
8
% OUTPUTS
9
10
11
12
13
%  o freq        integer scalar equal to 1, 4, 12 or 52 (for annual, quaterly, monthly or weekly frequencies).
%  o init        dates object, initial date in the dataset.
%  o data        matrix of doubles, the data.
%  o varlist     cell of strings, names of the variables.
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
14
15
16
% REMARKS
% The frequency and initial date can be specified with variables FREQ__ and INIT__ in the matlab/octave script. FREQ__ must
% be a scalar integer and INIT__ a string like '1938M11', '1945Q3', '1973W3' or '2009A'. If these variables are not specified
17
18
% default values for freq and init are 1 and dates(1,1).

Stéphane Adjemian's avatar
Stéphane Adjemian committed
19
% Copyright (C) 2012-2018 Dynare Team
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
%
% 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/>.

if isoctave
    run(file);
else
    basename = file(1:end-2);
    run(basename);
end

Stéphane Adjemian's avatar
Stéphane Adjemian committed
43
clear file basename;
44
45
46
if isoctave
    clear argn;
end
47

48
save tmp__dataSet__.mat;
49

Stéphane Adjemian's avatar
Stéphane Adjemian committed
50
[freq, init, data, varlist, tex, ops, tags] = load_mat_file_data('tmp__dataSet__.mat');
51

Stéphane Adjemian's avatar
Stéphane Adjemian committed
52
delete tmp__dataSet__.mat;
53

Stéphane Adjemian's avatar
Stéphane Adjemian committed
54
return
55

Stéphane Adjemian's avatar
Stéphane Adjemian committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
%@test:1
 % Create a data m-file
 fid = fopen('data_m_file.m','w');
 fprintf(fid,'FREQ__ = 4;');
 fprintf(fid,'INIT__ = ''1938Q4'';');
 fprintf(fid,'NAMES__ = {''azert'';''yuiop''};');
 fprintf(fid,'TEX__ = {''azert'';''yuiop''};');
 fprintf(fid,'OPS__ = {''method1(azert)'';''method2(yuiop)''};');
 fprintf(fid,'TAGS__ = struct();');
 fprintf(fid,'TAGS__.type = cell(2, 1);');
 fprintf(fid,'TAGS__.type(1) = {''Haut''};');
 fprintf(fid,'TAGS__.type(2) = {''Bas''};');
 fprintf(fid,'azert = [1; 2; 3; 4; 5];');
 fprintf(fid,'yuiop = [2; 3; 4; 5; 6];');
 fclose(fid);
71

Stéphane Adjemian's avatar
Stéphane Adjemian committed
72
73
74
75
76
 % Try to read the data m-file
     datafile = 'data_m_file.m';
     [freq, init, data, varlist, tex, ops, tags] = load_m_file_data(datafile);
     delete('data_m_file.m');
     t(1) = 1;
77

Stéphane Adjemian's avatar
Stéphane Adjemian committed
78
79
80
81
82
83
84
85
86
87
88
89
 % Check the results.
 t(2) = dassert(freq,4);
 t(3) = isdates(init);
 t(4) = dassert(init.freq,4);
 t(5) = dassert(init.time,[1938 4]);
 t(6) = dassert(varlist,{'azert';'yuiop'});
 t(7) = dassert(tex,{'azert';'yuiop'});
 t(8) = dassert(ops,{'method1(azert)';'method2(yuiop)'});
 t(9) = dassert(tags.type,{'Haut';'Bas'});
 t(10) = dassert(data(:,1),[1;2;3;4;5]);
 t(11) = dassert(data(:,2),[2;3;4;5;6]);
 T = all(t);
90
%@eof:1