Make get_equation_number_by_tag more robust

The implementation was relying on the fact that in M_.equations_tags, all
equations have a name tag and they appear in the order given by equation
numbers. There is no guarantee that this will always be the case, so use a more
robust approach.

By the way, improve the implementation of get_equation_name_by_number.
unit-tests
Sébastien Villemot 2022-10-26 14:16:53 +02:00
parent 5f6946a1b9
commit 720de52702
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
2 changed files with 16 additions and 7 deletions

View File

@ -18,11 +18,13 @@ function eqname = get_equation_name_by_number(eqnumber, M_)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
idx_for_this_eq = find(cell2mat(M_.equations_tags(:,1)) == eqnumber);
eqname = cell2mat(M_.equations_tags(idx_for_this_eq(strmatch('name', M_.equations_tags(idx_for_this_eq, 2), 'exact')), 3));
idx = find((cell2mat(M_.equations_tags(:,1)) == eqnumber) & ...
strcmp(M_.equations_tags(:,2), 'name'));
if isempty(eqname)
if isempty(idx)
eqname = '';
else
eqname = M_.equations_tags{idx, 3};
end
end

View File

@ -1,4 +1,4 @@
function eqnumber = get_equation_number_by_tag(eqname, DynareModel)
function eqnumber = get_equation_number_by_tag(eqname, M_)
% Translates an equation name into an equation number.
%
@ -9,7 +9,7 @@ function eqnumber = get_equation_number_by_tag(eqname, DynareModel)
% OUTPUTS
% - eqnumber [integer] Equation number.
% Copyright © 2018-2020 Dynare Team
% Copyright © 2018-2022 Dynare Team
%
% This file is part of Dynare.
%
@ -26,6 +26,13 @@ function eqnumber = get_equation_number_by_tag(eqname, DynareModel)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
eqnumber = strmatch(eqname, DynareModel.equations_tags(strmatch('name', DynareModel.equations_tags(:,2), 'exact'), 3), 'exact');
idx = find(strcmp(M_.equations_tags(:,2), 'name') & ...
strcmp(M_.equations_tags(:,3), eqname));
if isempty(eqnumber), eqnumber = 0; end
if isempty(idx)
eqnumber = 0;
else
eqnumber = M_.equations_tags{idx, 1};
end
end