1function o2WysrOISH  = load_m_file_data_legacy(datafile, U7ORsJ0vy3) % --*-- Unitary tests --*--
2
3% Copyright (C) 2014-2017 Dynare Team
4%
5% This file is part of Dynare.
6%
7% Dynare is free software: you can redistribute it and/or modify
8% it under the terms of the GNU General Public License as published by
9% the Free Software Foundation, either version 3 of the License, or
10% (at your option) any later version.
11%
12% Dynare is distributed in the hope that it will be useful,
13% but WITHOUT ANY WARRANTY; without even the implied warranty of
14% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15% GNU General Public License for more details.
16%
17% You should have received a copy of the GNU General Public License
18% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
19
20cXDHdrXnqo5KwwVpTRuc6OprAW = datafile(1:end-2);
21[pathtocXDHdrXnqo5KwwVpTRuc6OprAW,cXDHdrXnqo5KwwVpTRuc6OprAW,~] = fileparts(cXDHdrXnqo5KwwVpTRuc6OprAW);
22
23if ~isempty(pathtocXDHdrXnqo5KwwVpTRuc6OprAW)
24    % We need to change directory, first we keep the current directory in memory...
25    OvMuQsJgjwzYG5Pni0TzU8Acb2YBJva = pwd();
26    % Then we move in the directory where the data file is saved.
27    cd(pathtocXDHdrXnqo5KwwVpTRuc6OprAW);
28end
29
30% We evaluate the matlab script defining the data. All the variables in the
31% variables defined in this script are loaded in the current workspace.
32eval(cXDHdrXnqo5KwwVpTRuc6OprAW);
33
34if ~isempty(pathtocXDHdrXnqo5KwwVpTRuc6OprAW)
35    % If we previously changed directory, we go back to the initial directory.
36    cd(OvMuQsJgjwzYG5Pni0TzU8Acb2YBJva);
37    clear OvMuQsJgjwzYG5Pni0TzU8Acb2YBJva;
38end
39
40% Clear all the variables except the ones defined in the script.
41clear('pathtocXDHdrXnqo5KwwVpTRuc6OprAW', 'cXDHdrXnqo5KwwVpTRuc6OprAW');
42
43% Get the list of variables in the script.
44mj6F4eU1BN = whos();
45Z3s1ZFBffw = {mj6F4eU1BN(:).name}';
46
47% Check that the variables in varobs are available.
48if ~isequal(sort(intersect(Z3s1ZFBffw, U7ORsJ0vy3)), sort(U7ORsJ0vy3))
49    qtvUkxKk6b = setdiff(U7ORsJ0vy3, intersect(Z3s1ZFBffw, U7ORsJ0vy3));
50    qtvUkxKk6b = sprintf('%s ', qtvUkxKk6b{:});
51    qtvUkxKk6b = qtvUkxKk6b(1:end-1);
52    error('Some variables are missing (%s)!', qtvUkxKk6b)
53end
54
55% Check that the variables are provided as vectors.
56N5L9sgRHIu = {};
57for uAiwEPcc3Q=1:length(U7ORsJ0vy3)
58    if ~isvector(eval(U7ORsJ0vy3{uAiwEPcc3Q}))
59        N5L9sgRHIu(end+1) = {U7ORsJ0vy3{uAiwEPcc3Q}};
60    end
61end
62if ~isempty(N5L9sgRHIu)
63    N5L9sgRHIu = sprintf('%s ', N5L9sgRHIu{:});
64    N5L9sgRHIu = N5L9sgRHIu(1:end-1);
65    error('Observed variables should be provided as vectors (%s are not vectors)!')
66end
67
68% Check that all the vectors have the same number of elements.
69RXZzmKFPFK = numel(eval(U7ORsJ0vy3{1}));
70for uAiwEPcc3Q=2:length(U7ORsJ0vy3)
71    if ~isequal(numel(eval(U7ORsJ0vy3{1})), RXZzmKFPFK)
72        error('All vectors must have the same number of elements (%s has %i elements while %s has %i elements)!', U7ORsJ0vy3{1}, numel(eval(U7ORsJ0vy3{1})), U7ORsJ0vy3{uAiwEPcc3Q}, numel(eval(U7ORsJ0vy3{uAiwEPcc3Q})));
73    end
74end
75
76% Put the observed variables in data
77JSmvfqTSXT = repmat(' vec(%s) ', 1, length(U7ORsJ0vy3));
78VbO4y7zOlh = sprintf('[%s]', JSmvfqTSXT);
79o2WysrOISH = dseries(eval(sprintf(VbO4y7zOlh, U7ORsJ0vy3{:})), [], U7ORsJ0vy3);
80
81return
82
83%@test:1
84% Write a data file
85fid = fopen('example1.m','w');
86fwriten(fid, 'a = randn(100,1);');
87fwriten(fid, 'b = randn(100,1);');
88fwriten(fid, 'c = transpose(randn(100,1));');
89fwriten(fid, 'd = randn(100,1);');
90fwriten(fid, 'e = randn(100,2);');
91fwriten(fid, ' ');
92fwriten(fid, 'f = NaN(100,1);');
93fwriten(fid, 'for i=1:100');
94fwriten(fid, '  f(i) = log(rand());')
95fwriten(fid, 'end');
96fclose(fid);
97% Define a set of variables to be loaded.
98listofvariablestobeloaded = {'b'; 'a'};
99% Test if we can load the data.
100exist('example1.m', 'file'); % To force the detection of the new file under old MATLAB
101try
102    data = load_m_file_data_legacy('example1.m', listofvariablestobeloaded);
103    t(1) = 1;
104catch
105    t(1) = 0;
106end
107delete('example1.m');
108T = all(t);
109%@eof:1
110
111%@test:2
112% Write a data file
113fid = fopen('example2.m','w');
114fwriten(fid, 'a = randn(100,1);');
115fwriten(fid, 'b = randn(100,1);');
116fwriten(fid, 'c = transpose(randn(100,1));');
117fwriten(fid, 'd = randn(100,1);');
118fwriten(fid, 'e = randn(100,2);');
119fwriten(fid, ' ');
120fwriten(fid, 'f = NaN(100,1);');
121fwriten(fid, 'for i=1:100');
122fwriten(fid, '  f(i) = log(rand());')
123fwriten(fid, 'end');
124fclose(fid);
125% Define a set of variables to be loaded.
126listofvariablestobeloaded = {'e'; 'a'};
127% Test if we can load the data.
128exist('example2.m', 'file'); % To force the detection of the new file under old MATLAB
129try
130    data = load_m_file_data_legacy('example2.m', listofvariablestobeloaded);
131    t(1) = 0;
132catch
133    t(1) = 1;
134end
135delete('example2.m');
136T = all(t);
137%@eof:2
138
139%@test:3
140% Write a data file
141fid = fopen('example3.m','w');
142fwriten(fid, 'a = randn(100,1);');
143fwriten(fid, 'b = randn(100,1);');
144fwriten(fid, 'c = transpose(randn(100,1));');
145fwriten(fid, 'd = randn(100,1);');
146fwriten(fid, 'e = randn(100,2);');
147fwriten(fid, ' ');
148fwriten(fid, 'f = NaN(100,1);');
149fwriten(fid, 'for i=1:100');
150fwriten(fid, '  f(i) = log(rand());')
151fwriten(fid, 'end');
152fclose(fid);
153% Define a set of variables to be loaded.
154listofvariablestobeloaded = {'c'; 'a'};
155% Test if we can load the data.
156exist('example3.m', 'file'); % To force the detection of the new file under old MATLAB
157try
158    data = load_m_file_data_legacy('example3.m', listofvariablestobeloaded);
159    t(1) = 1;
160catch
161    t(1) = 0;
162end
163delete('example3.m');
164T = all(t);
165%@eof:3
166