diff --git a/src/@dseries/disp.m b/src/@dseries/disp.m index 6c458c9ceed99f72cae9d74777a03de5cee1e1c3..97f256bfac24fd1d9bd5c6d5681b0356b6e6e693 100644 --- a/src/@dseries/disp.m +++ b/src/@dseries/disp.m @@ -25,8 +25,15 @@ function disp(o) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -separator = repmat(' | ', nobs(o)+1,1); vspace = ' '; + +if ~vobs(o) + disp(vspace) + disp([inputname(1) ' is an empty dseries object.']) + return +end + +separator = repmat(' | ', nobs(o)+1,1); TABLE = ' '; for t=1:nobs(o) TABLE = char(TABLE, date2string(o.dates(t))); diff --git a/src/@dseries/display.m b/src/@dseries/display.m index 896847769a92ffd68774ac99cc0ac322214e06c2..6368a53a7fe561df994d35121d121875a099b10c 100644 --- a/src/@dseries/display.m +++ b/src/@dseries/display.m @@ -30,6 +30,13 @@ function display(o) % along with Dynare. If not, see <http://www.gnu.org/licenses/>. vspace = ' '; + +if ~vobs(o) + disp(vspace) + disp([inputname(1) ' is an empty dseries object.']) + return +end + TABLE = ' '; if vobs(o)<=10 diff --git a/src/@dseries/extract.m b/src/@dseries/extract.m index 956b30413abf3d47abd6166005823f1fad9cbcc1..663d3a29a8f87154aae4ce86e96c527612c5349d 100644 --- a/src/@dseries/extract.m +++ b/src/@dseries/extract.m @@ -19,6 +19,10 @@ function p = extract(o, varargin) % --*-- Unitary tests --*-- % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. +useimplicitloops = false; +useregularexpression = false; +usewildcardparameter = false; + p = dseries(); % Get the names of the variables to be extracted from dseries object B. @@ -30,21 +34,47 @@ for i=1:nargin-1 if mod(length(idArobase),2) error('dseries::extract: (Implicit loops) The number of @ symbols must be even!') end - % Regular expression + if ~isempty(idArobase) + useimplicitloops = true; + end + % Regular expression. idBracket.open = strfind(VariableName,'['); idBracket.close = strfind(VariableName,']'); - if ~isequal(length(idBracket.open),length(idBracket.open)) + if ~isempty(idBracket.open) && ~isempty(idBracket.close) + if isequal(idBracket.open(1), 1) && isequal(idBracket.close(end), length(VariableName)) + useregularexpression = true; + end + end + % Wildcard parameter. + if ~useregularexpression + idStar = strfind(VariableName,'~'); + if ~isempty(idStar) + usewildcardparameter = true; + useregularexpression = true; + VariableName = sprintf('[%s]', VariableName); + VariableName = strrep(VariableName, '~', '\w*'); + end + end + % Check that square brackets are not used, unless extract method is called with a regular expression. + if ~useregularexpression && ~(isempty(idBracket.open) && isempty(idBracket.close)) + error('dseries::extract: Square brackets are not allowed, unless regexp are used to select variables!') + end + if useregularexpression && usewildcardparameter && ~(isempty(idBracket.open) && isempty(idBracket.close)) + error('dseries::extract: Square brackets are not allowed, unless regexp are used to select variables!') + end + % Check that square brackets in regular expressions + if useregularexpression && ~usewildcardparameter && ~isequal(length(idBracket.open),length(idBracket.open)) error('dseries::extract: (Matlab/Octave''s regular expressions) Check opening and closing square brackets!') end % Loops and regular expressions are not compatible - if length(idArobase) && length(idBracket.open) - error(['dseries::extract: You cannot use implicit loops and regular expressions in the same rule!']) + if useregularexpression && useimplicitloops + error('dseries::extract: You cannot use implicit loops and regular expressions in the same rule!') end % Update the list of variables. - if length(idArobase) + if useimplicitloops VariableName_ = build_list_of_variables_with_loops(o.name, idArobase, VariableName, VariableName_); - elseif length(idBracket.open) - VariableName_ = build_list_of_variables_with_regexp(o.name, idBracket, VariableName, VariableName_); + elseif useregularexpression + VariableName_ = build_list_of_variables_with_regexp(o.name, VariableName(2:end-1), usewildcardparameter); else VariableName_ = varargin(:); end diff --git a/src/@dseries/lineartrend.m b/src/@dseries/lineartrend.m index 96bb8a5a9c509c860ab08feccfb1528d344747e4..593ac2c1a07416259917bf154c1f1871347dd928 100644 --- a/src/@dseries/lineartrend.m +++ b/src/@dseries/lineartrend.m @@ -35,7 +35,7 @@ return %@test:1 a = dseries(randn(100,3)); try - trend = o.lineartrend; + trend = a.lineartrend; t(1) = 1; catch t(1) = 0; diff --git a/src/utilities/variables/build_list_of_variables_with_regexp.m b/src/utilities/variables/build_list_of_variables_with_regexp.m index acf907b693d5e9f3b586cb00400679f2815a3f02..6b9cd3820cde3f43cbf6aafa8b71f96880c17c21 100644 --- a/src/utilities/variables/build_list_of_variables_with_regexp.m +++ b/src/utilities/variables/build_list_of_variables_with_regexp.m @@ -1,4 +1,4 @@ -function list_of_variables = build_list_of_variables_with_regexp(o_list_of_variables, idBrackets, VariableName, list_of_variables) +function list_of_variables = build_list_of_variables_with_regexp(o_list_of_variables, VariableName, wildcardparameterflag) % Copyright (C) 2016-2017 Dynare Team % @@ -17,40 +17,22 @@ function list_of_variables = build_list_of_variables_with_regexp(o_list_of_varia % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -first_block_id = 0; -last_block_id = 0; +matched_strings = regexp(o_list_of_variables, VariableName, 'match'); +matched_strings_ = {}; -idVariables = find(isnotempty_cell(regexp(o_list_of_variables,VariableName,'match'))); - -if isempty(idVariables) - error(['dseries::regexp: Can''t find any variable matching ' VariableName ' pattern!']) -end - -idVariables_ = []; - -for j = 1:length(idVariables) - first_block_flag = 0; - if (first_block_id && strcmp(o_list_of_variables{idVariables(j)}(1:first_block_id),VariableName(1:first_block_id))) || ~first_block_id - first_block_flag = 1; - end - last_block_flag = 0; - if (last_block_id && strcmp(o_list_of_variables{idVariables(j)}(end-last_block_id:end),VariableName(end-last_block_id:end))) || ~last_block_id - last_block_flag = 1; - end - if first_block_flag && last_block_flag - idVariables_ = [idVariables_; idVariables(j)]; +for i=1:length(matched_strings) + if ~isempty(matched_strings{i}) + matched_strings_ = [matched_strings_, matched_strings{i}(1)]; end end -VariableName = o_list_of_variables(idVariables_); -list_of_variables = vertcat(list_of_variables, VariableName); +list_of_variables = intersect(o_list_of_variables, matched_strings_); - -function b = isnotempty_cell(CellArray) -CellArrayDimension = size(CellArray); -b = NaN(CellArrayDimension); -for i=1:CellArrayDimension(1) - for j = 1:CellArrayDimension(2) - b(i,j) = ~isempty(CellArray{i,j}); +if isempty(list_of_variables) + if wildcardparameterflag + VariableName = strrep(VariableName, '\w*', '*'); + disp(['dseries::extact: The wildcard expression ''' VariableName ''' did not match any variable name!']) + else + disp(['dseries::extact: The regular expression ''[' VariableName ']'' did not match any variable name!']) end -end +end \ No newline at end of file