diff --git a/src/@dseries/extract.m b/src/@dseries/extract.m index 536a63f92f91cf4ef40154e0e51865a53d7b84ce..51caec631a2454025c50273a54c9ee7c71124b39 100644 --- a/src/@dseries/extract.m +++ b/src/@dseries/extract.m @@ -21,6 +21,7 @@ function p = extract(o, varargin) % --*-- Unitary tests --*-- useimplicitloops = false; useregularexpression = false; +usewildcardparameter = false; p = dseries(); @@ -36,18 +37,33 @@ for i=1:nargin-1 if ~isempty(idArobase) useimplicitloops = true; end - % Regular expression + % Regular expression. idBracket.open = strfind(VariableName,'['); idBracket.close = strfind(VariableName,']'); - if isequal(idBracket.open(1), 1) && isequal(idBracket.close(end), length(VariableName)) - useregularexpression = true; + if ~isempty(idBracket.open) && ~isempty(idBracket.close) + if isequal(idBracket.open(1), 1) && isequal(idBracket.close(end), length(VariableName)) + useregularexpression = true; + end end - % Check that square brackets are not used, unless extract method is called with a regular expression. + % 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 && ~isequal(length(idBracket.open),length(idBracket.open)) + 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 @@ -58,7 +74,7 @@ for i=1:nargin-1 if useimplicitloops VariableName_ = build_list_of_variables_with_loops(o.name, idArobase, VariableName, VariableName_); elseif useregularexpression - VariableName_ = build_list_of_variables_with_regexp(o.name, VariableName(2:end-1)); + VariableName_ = build_list_of_variables_with_regexp(o.name, VariableName(2:end-1), usewildcardparameter); else VariableName_ = varargin(:); end 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 b65703af714b85445f9a0104a6ebc020de68099f..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, VariableName) +function list_of_variables = build_list_of_variables_with_regexp(o_list_of_variables, VariableName, wildcardparameterflag) % Copyright (C) 2016-2017 Dynare Team % @@ -29,5 +29,10 @@ end list_of_variables = intersect(o_list_of_variables, matched_strings_); if isempty(list_of_variables) - disp(['dseries::extact: The regular expression ''[' VariableName ']'' did not match any variable name!']) + 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 \ No newline at end of file