2017-05-10 16:35:58 +02:00
function o2WysrOISH = load_m_file_data_legacy ( datafile, U7ORsJ0vy3) % --*-- Unitary tests --*--
2014-10-23 11:08:26 +02:00
2017-05-10 16:35:58 +02:00
% Copyright (C) 2014-2017 Dynare Team
2014-10-20 17:28:35 +02:00
%
% 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/>.
2014-10-21 16:05:34 +02:00
cXDHdrXnqo5KwwVpTRuc6OprAW = datafile ( 1 : end - 2 ) ;
2018-11-13 17:58:42 +01:00
[ pathtocXDHdrXnqo5KwwVpTRuc6OprAW , cXDHdrXnqo5KwwVpTRuc6OprAW , ~ ] = fileparts ( cXDHdrXnqo5KwwVpTRuc6OprAW ) ;
2014-10-21 16:05:34 +02:00
2014-10-23 11:08:26 +02:00
if ~ isempty ( pathtocXDHdrXnqo5KwwVpTRuc6OprAW )
2017-05-10 16:35:58 +02:00
% We need to change directory, first we keep the current directory in memory...
2014-10-23 11:08:26 +02:00
OvMuQsJgjwzYG5Pni0TzU8Acb2YBJva = pwd ( ) ;
2017-05-10 16:35:58 +02:00
% Then we move in the directory where the data file is saved.
2014-10-23 11:08:26 +02:00
cd ( pathtocXDHdrXnqo5KwwVpTRuc6OprAW ) ;
end
2017-05-10 16:35:58 +02:00
% We evaluate the matlab script defining the data. All the variables in the
% variables defined in this script are loaded in the current workspace.
2014-10-21 16:05:34 +02:00
eval ( cXDHdrXnqo5KwwVpTRuc6OprAW ) ;
2014-10-23 11:08:26 +02:00
if ~ isempty ( pathtocXDHdrXnqo5KwwVpTRuc6OprAW )
2017-05-10 16:35:58 +02:00
% If we previously changed directory, we go back to the initial directory.
2014-10-23 11:08:26 +02:00
cd ( OvMuQsJgjwzYG5Pni0TzU8Acb2YBJva ) ;
2017-05-10 16:35:58 +02:00
clear OvMuQsJgjwzYG5Pni0TzU8Acb2YBJva ;
2014-10-23 11:08:26 +02:00
end
2014-10-21 16:05:34 +02:00
2017-05-10 16:35:58 +02:00
% Clear all the variables except the ones defined in the script.
2018-11-13 17:58:42 +01:00
clear ( ' pathtocXDHdrXnqo5KwwVpTRuc6OprAW' , ' cXDHdrXnqo5KwwVpTRuc6OprAW' ) ;
2017-05-10 16:35:58 +02:00
% Get the list of variables in the script.
mj6F4eU1BN = whos ( ) ;
2019-03-26 17:22:18 +01:00
Z3s1ZFBffw = { mj6F4eU1BN ( : ) . name } ' ;
2017-05-10 16:35:58 +02:00
% Check that the variables in varobs are available.
if ~ isequal ( sort ( intersect ( Z3s1ZFBffw , U7ORsJ0vy3 ) ) , sort ( U7ORsJ0vy3 ) )
qtvUkxKk6b = setdiff ( U7ORsJ0vy3 , intersect ( Z3s1ZFBffw , U7ORsJ0vy3 ) ) ;
qtvUkxKk6b = sprintf ( ' %s ' , qtvUkxKk6b { : } ) ;
qtvUkxKk6b = qtvUkxKk6b ( 1 : end - 1 ) ;
error ( ' Some variables are missing (%s)!' , qtvUkxKk6b )
end
2017-05-10 18:42:46 +02:00
% Check that the variables are provided as vectors.
2017-05-10 16:35:58 +02:00
N5L9sgRHIu = { } ;
for uAiwEPcc3Q = 1 : length ( U7ORsJ0vy3 )
if ~ isvector ( eval ( U7ORsJ0vy3 { uAiwEPcc3Q } ) )
N5L9sgRHIu ( end + 1 ) = { U7ORsJ0vy3 { uAiwEPcc3Q } } ;
end
end
if ~ isempty ( N5L9sgRHIu )
N5L9sgRHIu = sprintf ( ' %s ' , N5L9sgRHIu { : } ) ;
N5L9sgRHIu = N5L9sgRHIu ( 1 : end - 1 ) ;
error ( ' Observed variables should be provided as vectors (%s are not vectors)!' )
2014-10-20 17:28:35 +02:00
end
2017-05-10 16:35:58 +02:00
% Check that all the vectors have the same number of elements.
RXZzmKFPFK = numel ( eval ( U7ORsJ0vy3 { 1 } ) ) ;
for uAiwEPcc3Q = 2 : length ( U7ORsJ0vy3 )
if ~ isequal ( numel ( eval ( U7ORsJ0vy3 { 1 } ) ) , RXZzmKFPFK )
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 } ) ) ) ;
2014-10-20 17:28:35 +02:00
end
2017-05-10 16:35:58 +02:00
end
% Put the observed variables in data
JSmvfqTSXT = repmat ( ' vec(%s) ' , 1 , length ( U7ORsJ0vy3 ) ) ;
VbO4y7zOlh = sprintf ( ' [%s]' , JSmvfqTSXT ) ;
o2WysrOISH = dseries ( eval ( sprintf ( VbO4y7zOlh , U7ORsJ0vy3 { : } ) ) , [ ] , U7ORsJ0vy3 ) ;
return
%@test:1
2017-05-16 15:10:20 +02:00
% Write a data file
2018-07-18 12:18:43 +02:00
fid = fopen ( ' example1.m' , ' w' ) ;
2017-05-16 15:10:20 +02:00
fwriten ( fid , ' a = randn(100,1);' ) ;
fwriten ( fid , ' b = randn(100,1);' ) ;
fwriten ( fid , ' c = transpose(randn(100,1));' ) ;
fwriten ( fid , ' d = randn(100,1);' ) ;
fwriten ( fid , ' e = randn(100,2);' ) ;
fwriten ( fid , ' ' ) ;
fwriten ( fid , ' f = NaN(100,1);' ) ;
fwriten ( fid , ' for i=1:100' ) ;
fwriten ( fid , ' f(i) = log(rand());' )
fwriten ( fid , ' end' ) ;
fclose ( fid ) ;
% Define a set of variables to be loaded.
2019-03-26 17:22:18 +01:00
listofvariablestobeloaded = { ' b' ; ' a' } ;
2017-05-16 15:10:20 +02:00
% Test if we can load the data.
try
2018-07-18 12:18:43 +02:00
data = load_m_file_data_legacy ( ' example1.m' , listofvariablestobeloaded ) ;
delete ( ' example1.m' ) ;
2017-05-16 15:10:20 +02:00
t ( 1 ) = 1 ;
catch
t ( 1 ) = 0 ;
end
T = all ( t ) ;
2017-05-10 16:35:58 +02:00
%@eof:1
%@test:2
2017-05-16 15:10:20 +02:00
% Write a data file
2018-07-18 12:18:43 +02:00
fid = fopen ( ' example2.m' , ' w' ) ;
2017-05-16 15:10:20 +02:00
fwriten ( fid , ' a = randn(100,1);' ) ;
fwriten ( fid , ' b = randn(100,1);' ) ;
fwriten ( fid , ' c = transpose(randn(100,1));' ) ;
fwriten ( fid , ' d = randn(100,1);' ) ;
fwriten ( fid , ' e = randn(100,2);' ) ;
fwriten ( fid , ' ' ) ;
fwriten ( fid , ' f = NaN(100,1);' ) ;
fwriten ( fid , ' for i=1:100' ) ;
fwriten ( fid , ' f(i) = log(rand());' )
fwriten ( fid , ' end' ) ;
fclose ( fid ) ;
% Define a set of variables to be loaded.
2019-03-26 17:22:18 +01:00
listofvariablestobeloaded = { ' e' ; ' a' } ;
2017-05-16 15:10:20 +02:00
% Test if we can load the data.
try
2018-07-18 12:18:43 +02:00
data = load_m_file_data_legacy ( ' example2.m' , listofvariablestobeloaded ) ;
delete ( ' example2.m' ) ;
2017-05-16 15:10:20 +02:00
t ( 1 ) = 0 ;
catch
t ( 1 ) = 1 ;
end
T = all ( t ) ;
2017-05-10 16:35:58 +02:00
%@eof:2
%@test:3
2017-05-16 15:10:20 +02:00
% Write a data file
2018-07-18 12:18:43 +02:00
fid = fopen ( ' example3.m' , ' w' ) ;
2017-05-16 15:10:20 +02:00
fwriten ( fid , ' a = randn(100,1);' ) ;
fwriten ( fid , ' b = randn(100,1);' ) ;
fwriten ( fid , ' c = transpose(randn(100,1));' ) ;
fwriten ( fid , ' d = randn(100,1);' ) ;
fwriten ( fid , ' e = randn(100,2);' ) ;
fwriten ( fid , ' ' ) ;
fwriten ( fid , ' f = NaN(100,1);' ) ;
fwriten ( fid , ' for i=1:100' ) ;
fwriten ( fid , ' f(i) = log(rand());' )
fwriten ( fid , ' end' ) ;
fclose ( fid ) ;
% Define a set of variables to be loaded.
2019-03-26 17:22:18 +01:00
listofvariablestobeloaded = { ' c' ; ' a' } ;
2017-05-16 15:10:20 +02:00
% Test if we can load the data.
try
2018-07-18 12:18:43 +02:00
data = load_m_file_data_legacy ( ' example3.m' , listofvariablestobeloaded ) ;
delete ( ' example3.m' ) ;
2017-05-16 15:10:20 +02:00
t ( 1 ) = 1 ;
catch
t ( 1 ) = 0 ;
end
T = all ( t ) ;
2017-05-10 16:35:58 +02:00
%@eof:3