1function p = extract(o, varargin) % --*-- Unitary tests --*-- 2 3% Extract some variables from a database. 4 5% Copyright (C) 2012-2017 Dynare Team 6% 7% This file is part of Dynare. 8% 9% Dynare is free software: you can redistribute it and/or modify 10% it under the terms of the GNU General Public License as published by 11% the Free Software Foundation, either version 3 of the License, or 12% (at your option) any later version. 13% 14% Dynare is distributed in the hope that it will be useful, 15% but WITHOUT ANY WARRANTY; without even the implied warranty of 16% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17% GNU General Public License for more details. 18% 19% You should have received a copy of the GNU General Public License 20% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 21 22useimplicitloops = false; 23useregularexpression = false; 24usewildcardparameter = false; 25 26p = dseries(); 27 28% Get the names of the variables to be extracted from dseries object B. 29VariableName_ = {}; 30for i=1:nargin-1 31 VariableName = varargin{i}; 32 % Implicit loop. 33 idArobase = strfind(VariableName,'@'); 34 if mod(length(idArobase),2) 35 error('dseries::extract: (Implicit loops) The number of @ symbols must be even!') 36 end 37 if ~isempty(idArobase) 38 useimplicitloops = true; 39 end 40 % Regular expression. 41 idBracket.open = strfind(VariableName,'['); 42 idBracket.close = strfind(VariableName,']'); 43 if ~isempty(idBracket.open) && ~isempty(idBracket.close) 44 if isequal(idBracket.open(1), 1) && isequal(idBracket.close(end), length(VariableName)) 45 useregularexpression = true; 46 end 47 end 48 % Wildcard parameter. 49 if ~useregularexpression 50 idStar = strfind(VariableName,'~'); 51 if ~isempty(idStar) 52 usewildcardparameter = true; 53 useregularexpression = true; 54 VariableName = sprintf('[%s]', VariableName); 55 VariableName = strrep(VariableName, '~', '\w*'); 56 end 57 end 58 % Check that square brackets are not used, unless extract method is called with a regular expression. 59 if ~useregularexpression && ~(isempty(idBracket.open) && isempty(idBracket.close)) 60 error('dseries::extract: Square brackets are not allowed, unless regexp are used to select variables!') 61 end 62 if useregularexpression && usewildcardparameter && ~(isempty(idBracket.open) && isempty(idBracket.close)) 63 error('dseries::extract: Square brackets are not allowed, unless regexp are used to select variables!') 64 end 65 % Check that square brackets in regular expressions 66 if useregularexpression && ~usewildcardparameter && ~isequal(length(idBracket.open),length(idBracket.open)) 67 error('dseries::extract: (MATLAB/Octave''s regular expressions) Check opening and closing square brackets!') 68 end 69 % Loops and regular expressions are not compatible 70 if useregularexpression && useimplicitloops 71 error('dseries::extract: You cannot use implicit loops and regular expressions in the same rule!') 72 end 73 % Update the list of variables. 74 if useimplicitloops 75 VariableName_ = build_list_of_variables_with_loops(o.name, idArobase, VariableName, VariableName_); 76 elseif useregularexpression 77 VariableName_ = build_list_of_variables_with_regexp(o.name, VariableName(2:end-1), usewildcardparameter); 78 else 79 VariableName_ = varargin(:); 80 end 81end 82 83% Remove trailing white spaces if any 84VariableName_ = strtrim(VariableName_); 85 86% Get indices of the selected variables 87idVariableName = NaN(length(VariableName_),1); 88for i = 1:length(idVariableName) 89 idx = find(strcmp(VariableName_{i},o.name)); 90 if isempty(idx) 91 error(['dseries::extract: Variable ' VariableName_{i} ' is not a member of ' inputname(1) '!']) 92 end 93 idVariableName(i) = idx; 94end 95 96p.data = o.data(:,idVariableName); 97p.dates = o.dates; 98p.name = o.name(idVariableName); 99p.tex = o.tex(idVariableName); 100p.ops = o.ops(idVariableName); 101 102%@test:1 103%$ % Define a data set. 104%$ A = rand(10,24); 105%$ 106%$ % Define names 107%$ A_name = {'GDP_1';'GDP_2';'GDP_3'; 'GDP_4'; 'GDP_5'; 'GDP_6'; 'GDP_7'; 'GDP_8'; 'GDP_9'; 'GDP_10'; 'GDP_11'; 'GDP_12'; 'HICP_1';'HICP_2';'HICP_3'; 'HICP_4'; 'HICP_5'; 'HICP_6'; 'HICP_7'; 'HICP_8'; 'HICP_9'; 'HICP_10'; 'HICP_11'; 'HICP_12';}; 108%$ 109%$ % Instantiate a time series object. 110%$ ts1 = dseries(A,[],A_name,[]); 111%$ 112%$ % Call the tested method. 113%$ a = ts1{'GDP_@1,2,3,4,5@'}; 114%$ b = ts1{'@GDP,HICP@_1'}; 115%$ 116%$ % Expected results. 117%$ e1.data = A(:,1:5); 118%$ e1.nobs = 10; 119%$ e1.vobs = 5; 120%$ e1.name = {'GDP_1';'GDP_2';'GDP_3'; 'GDP_4'; 'GDP_5'}; 121%$ e1.freq = 1; 122%$ e1.init = dates(1,1); 123%$ e2.data = A(:,[1, 13]); 124%$ e2.nobs = 10; 125%$ e2.vobs = 2; 126%$ e2.name = {'GDP_1';'HICP_1'}; 127%$ e2.freq = 1; 128%$ e2.init = dates(1,1); 129%$ 130%$ % Check results. 131%$ t(1) = dassert(e1.data,a.data); 132%$ t(2) = dassert(e1.nobs,a.nobs); 133%$ t(3) = dassert(e1.vobs,a.vobs); 134%$ t(4) = dassert(e1.name,a.name); 135%$ t(5) = dassert(e1.init,a.init); 136%$ t(6) = dassert(e2.data,b.data); 137%$ t(7) = dassert(e2.nobs,b.nobs); 138%$ t(8) = dassert(e2.vobs,b.vobs); 139%$ t(9) = dassert(e2.name,b.name); 140%$ t(10) = dassert(e2.init,b.init); 141%$ T = all(t); 142%@eof:1 143 144 145%@test:2 146%$ % Define a data set. 147%$ A = rand(10,24); 148%$ 149%$ % Define names 150%$ A_name = {'GDP_1';'GDP_2';'GDP_3'; 'GDP_4'; 'GDP_5'; 'GDP_6'; 'GDP_7'; 'GDP_8'; 'GDP_9'; 'GDP_10'; 'GDP_11'; 'GDP_12'; 'HICP_1';'HICP_2';'HICP_3'; 'HICP_4'; 'HICP_5'; 'HICP_6'; 'HICP_7'; 'HICP_8'; 'HICP_9'; 'HICP_10'; 'HICP_11'; 'HICP_12';}; 151%$ 152%$ % Instantiate a time series object. 153%$ ts1 = dseries(A,[],A_name,[]); 154%$ 155%$ % Call the tested method. 156%$ try 157%$ a = ts1{'GDP_@1,2,3,4,55@'}; 158%$ t = 0; 159%$ catch 160%$ t = 1; 161%$ end 162%$ 163%$ T = all(t); 164%@eof:2 165 166 167%@test:3 168%$ % Define a data set. 169%$ A = rand(10,24); 170%$ 171%$ % Define names 172%$ A_name = {'GDP_1';'GDP_2';'GDP_3'; 'GDP_4'; 'GDP_5'; 'GDP_6'; 'GDP_7'; 'GDP_8'; 'GDP_9'; 'GDP_10'; 'GDP_11'; 'GDP_12'; 'HICP_1';'HICP_2';'HICP_3'; 'HICP_4'; 'HICP_5'; 'HICP_6'; 'HICP_7'; 'HICP_8'; 'HICP_9'; 'HICP_10'; 'HICP_11'; 'HICP_12';}; 173%$ 174%$ % Instantiate a time series object. 175%$ ts1 = dseries(A,[],A_name,[]); 176%$ 177%$ % Call the tested method. 178%$ try 179%$ a = ts1{'@GDP,HICP@_@1,2,3,4,5@'}; 180%$ t = 1; 181%$ catch 182%$ t = 0; 183%$ end 184%$ 185%$ if t(1) 186%$ t(2) = dassert(a.name,{'GDP_1';'GDP_2';'GDP_3';'GDP_4';'GDP_5';'HICP_1';'HICP_2';'HICP_3';'HICP_4';'HICP_5'}); 187%$ end 188%$ 189%$ T = all(t); 190%@eof:3