From cf4e5cb76d2ad20906bf3afeb17b7e29c10c9cd2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Scylla=29?=
<stephane.adjemian@univ-lemans.fr>
Date: Thu, 31 Aug 2017 19:36:42 +0200
Subject: [PATCH] Added the possibility to select variables with wildcard
parameters (*).
---
src/@dseries/extract.m | 28 +++++++++++++++----
.../build_list_of_variables_with_regexp.m | 9 ++++--
2 files changed, 29 insertions(+), 8 deletions(-)
diff --git a/src/@dseries/extract.m b/src/@dseries/extract.m
index 536a63f..51caec6 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 b65703a..6b9cd38 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
--
GitLab