Verified Commit 74167124 authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Closes #16.

parent 677c1e18
...@@ -16,7 +16,7 @@ function [freq, init, data, varlist, tex, ops, tags] = load_m_file_data(file) % ...@@ -16,7 +16,7 @@ function [freq, init, data, varlist, tex, ops, tags] = load_m_file_data(file) %
% be a scalar integer and INIT__ a string like '1938M11', '1945Q3', '1973W3' or '2009A'. If these variables are not specified % be a scalar integer and INIT__ a string like '1938M11', '1945Q3', '1973W3' or '2009A'. If these variables are not specified
% default values for freq and init are 1 and dates(1,1). % default values for freq and init are 1 and dates(1,1).
% Copyright (C) 2012-2017 Dynare Team % Copyright (C) 2012-2018 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
...@@ -40,146 +40,55 @@ else ...@@ -40,146 +40,55 @@ else
run(basename); run(basename);
end end
if exist('INIT__','var') clear file basename;
if isdate(INIT__)
init = dates(INIT__);
clear('INIT__')
else
error('load_m_file_data: INIT__ cannot be interpreted as a date.')
end
else
init = dates(1,1); % Default initial date is year one.
end
if exist('FREQ__','var')
freq = FREQ__;
clear('FREQ__');
else
freq = init.freq;
end
if ~isequal(freq,init.freq) save tmp__dataSet__;
error('load_m_file_data: INIT__ and FREQ__ are not consistent!')
end
if exist('NAMES__','var') [freq, init, data, varlist, tex, ops, tags] = load_mat_file_data('tmp__dataSet__.mat');
varlist0 = NAMES__;
clear('NAMES__');
else
varlist0 = [];
list_of_variables = [];
end
if exist('TEX__','var') delete tmp__dataSet__.mat;
tex = TEX__;
clear('TEX__');
else
tex = [];
end
if exist('OPS__','var') return
ops = OPS__;
clear('OPS__');
else
ops = [];
end
if exist('TAGS__','var') %@test:1
tags = TAGS__; % Create a data m-file
clear('TAGS__'); fid = fopen('data_m_file.m','w');
else fprintf(fid,'FREQ__ = 4;');
tags = struct(); fprintf(fid,'INIT__ = ''1938Q4'';');
end fprintf(fid,'NAMES__ = {''azert'';''yuiop''};');
fprintf(fid,'TEX__ = {''azert'';''yuiop''};');
if isempty(varlist0) fprintf(fid,'OPS__ = {''method1(azert)'';''method2(yuiop)''};');
list_of_variables = whos(); fprintf(fid,'TAGS__ = struct();');
end fprintf(fid,'TAGS__.type = cell(2, 1);');
fprintf(fid,'TAGS__.type(1) = {''Haut''};');
data = []; fprintf(fid,'TAGS__.type(2) = {''Bas''};');
varlist = {}; fprintf(fid,'azert = [1; 2; 3; 4; 5];');
fprintf(fid,'yuiop = [2; 3; 4; 5; 6];');
if isempty(varlist0) fclose(fid);
for current_variable_index=1:length(list_of_variables)
if isequal(list_of_variables(current_variable_index).name,'freq') ...
|| isequal(list_of_variables(current_variable_index).name,'time') ...
|| isequal(list_of_variables(current_variable_index).name,'data') ...
|| isequal(list_of_variables(current_variable_index).name,'varlist') ...
|| isequal(list_of_variables(current_variable_index).name,'varlist0') ...
|| isequal(list_of_variables(current_variable_index).name,'list_of_variables') ...
|| isequal(list_of_variables(current_variable_index).name,'tex') ...
|| isequal(list_of_variables(current_variable_index).name,'ops') ...
|| isequal(list_of_variables(current_variable_index).name,'tags') ...
continue
end
if list_of_variables(current_variable_index).global || list_of_variables(current_variable_index).persistent
% A variable cannot be a global or persistent variable.
continue
end
if list_of_variables(current_variable_index).complex || ~strcmp(list_of_variables(current_variable_index).class,'double')
% A variable cannot be complex.
continue
end
if list_of_variables(current_variable_index).size(2)>1
% A variable must be passed as a column vector.
continue
end
try
eval(['data = [data, ' list_of_variables(current_variable_index).name '];'])
eval(['varlist = {varlist{:}, ''' list_of_variables(current_variable_index).name '''};'])
catch
error(['load_m_file:: All the vectors (variables) in ' inputname(1) ' must have the same number of rows (observations)!'])
end
end
else
for current_variable_index=1:length(varlist0)
eval(['data = [data, ' varlist0{current_variable_index} '];'])
end
varlist = varlist0;
end
if isempty(ops) % Try to read the data m-file
ops = cell(length(varlist), 1); try
end 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;
catch exception
t(1) = 0;
T = all(t);
LOG = getReport(exception,'extended');
return
end
%@test:1 % Check the results.
%$ % Create a data m-file t(2) = dassert(freq,4);
%$ fid = fopen('data_m_file.m','w'); t(3) = isdates(init);
%$ fprintf(fid,'FREQ__ = 4;'); t(4) = dassert(init.freq,4);
%$ fprintf(fid,'INIT__ = ''1938Q4'';'); t(5) = dassert(init.time,[1938 4]);
%$ fprintf(fid,'NAMES__ = {''azert'';''yuiop''};'); t(6) = dassert(varlist,{'azert';'yuiop'});
%$ fprintf(fid,'TEX__ = {''azert'';''yuiop''};'); t(7) = dassert(tex,{'azert';'yuiop'});
%$ fprintf(fid,'OPS__ = {''method1(azert)'';''method2(yuiop)''};'); t(8) = dassert(ops,{'method1(azert)';'method2(yuiop)'});
%$ fprintf(fid,'TAGS__ = struct();'); t(9) = dassert(tags.type,{'Haut';'Bas'});
%$ fprintf(fid,'TAGS__.type = cell(2, 1);'); t(10) = dassert(data(:,1),[1;2;3;4;5]);
%$ fprintf(fid,'TAGS__.type(1) = {''Haut''};'); t(11) = dassert(data(:,2),[2;3;4;5;6]);
%$ fprintf(fid,'TAGS__.type(2) = {''Bas''};'); T = all(t);
%$ fprintf(fid,'azert = [1; 2; 3; 4; 5];');
%$ fprintf(fid,'yuiop = [2; 3; 4; 5; 6];');
%$ fclose(fid);
%$
%$ % Try to read the data m-file
%$ try
%$ 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;
%$ catch exception
%$ t(1) = 0;
%$ T = all(t);
%$ LOG = getReport(exception,'extended');
%$ return
%$ end
%$
%$ % 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);
%@eof:1 %@eof:1
...@@ -3,20 +3,20 @@ function [freq, init, data, varlist, tex, ops, tags] = load_mat_file_data(file) ...@@ -3,20 +3,20 @@ function [freq, init, data, varlist, tex, ops, tags] = load_mat_file_data(file)
% Loads data in a matlab/octave mat-file. % Loads data in a matlab/octave mat-file.
% %
% INPUTS % INPUTS
% o file string, name of the matlab/octave mat file (with path) % - file string, name of the matlab/octave mat file (with path)
% %
% OUTPUTS % OUTPUTS
% o freq integer scalar equal to 1, 4, 12 or 52 (for annual, quaterly, monthly or weekly frequencies). % - 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. % - init dates object, initial date in the dataset.
% o data matrix of doubles, the data. % - data matrix of doubles, the data.
% o varlist cell of strings, names of the variables. % - varlist cell of strings, names of the variables.
% %
% REMARKS % REMARKS
% The frequency and initial date can be specified with variables FREQ__ and INIT__ in the matlab/octave binary file. FREQ__ must % The frequency and initial date can be specified with variables FREQ__ and INIT__ in the matlab/octave binary file. FREQ__ must
% be a scalar integer and INIT__ a string like '1938M11', '1945Q3', '1973W3' or '2009A'. If these variables are not specified % be a scalar integer and INIT__ a string like '1938M11', '1945Q3', '1973W3' or '2009A'. If these variables are not specified
% default values for freq and init are 1 and dates(1,1). % default values for freq and init are 1 and dates(1,1).
% Copyright (C) 2012-2017 Dynare Team % Copyright (C) 2012-2018 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
...@@ -92,10 +92,14 @@ end ...@@ -92,10 +92,14 @@ end
for i=1:length(varlist) for i=1:length(varlist)
try try
tmp = getfield(datafile,varlist{i}); tmp = datafile.(varlist{i});
data = [data, tmp(:)]; if isvector(tmp)
data = [data, tmp(:)];
else
error('load_mat_file:: All the variables must be vectors (%s is not a vector)!', varlist{i})
end
catch catch
error(['load_mat_file:: All the vectors (variables) in ' inputname(1) ' must have the same number of rows (observations)!']) error('load_mat_file:: All the vectors (variables) in %s must have the same number of rows (observations)!', inputname(1))
end end
end end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment