From 4b4de4102adcc493f15dbc54f284b108cadb30b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Scylla=29?= Date: Mon, 3 Feb 2014 12:18:23 +0100 Subject: [PATCH] Added new routine that converts a string of Key-Value pairs in a cell. --- matlab/read_key_value_string.m | 67 +++++++++++++++++++++++++++++++ matlab/utilities/general/iseven.m | 20 +++++++++ matlab/utilities/general/isodd.m | 20 +++++++++ 3 files changed, 107 insertions(+) create mode 100644 matlab/read_key_value_string.m create mode 100644 matlab/utilities/general/iseven.m create mode 100644 matlab/utilities/general/isodd.m diff --git a/matlab/read_key_value_string.m b/matlab/read_key_value_string.m new file mode 100644 index 000000000..a3b7243b6 --- /dev/null +++ b/matlab/read_key_value_string.m @@ -0,0 +1,67 @@ +function c = read_key_value_string(s) + +% Transforms a string of Key-Value options as returned by the preprocessor (for optim option in the +% estimation command) into a cell (first column for the option name ans second column for the +% option value). + +% Copyright (C) 2011-2013 Dynare Team +% +% This file is part of Dynare. +% +% Dynare is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Dynare. If not, see . + +iComma = strfind(s,','); +nComma = length(iComma); + +if iseven(nComma) + error('read_key_value_string: Wrong number of Key-Value pairs!') +end + +c = cell((nComma+1)/2,2); + +for i = 1:nComma + j = comma2opt(i); + if j>0, continue, end + if isequal(i,1) + i1 = 1; + i2 = iComma(i)-1; + else + i1 = iComma(i-1)+1; + i2 = iComma(i)-1; + end + if isequal(i,nComma) + i3 = iComma(i)+1; + i4 = length(s); + else + i3 = iComma(i)+1; + i4 = iComma(i+1)-1; + end + c(-j,1) = {s(i1+1:i2-1)}; + tmp = str2num(s(i3:i4)); + if isempty(tmp) + c(-j,2) = {s(i3+1:i4-1)}; + else + c(-j,2) = {tmp}; + end +end + + +function j = comma2opt(i) + if isodd(i) + % The comma is a separator between a Key and a Value (returned j is minus the option number). + j = - (i+1)/2; + else + % The comma is a separator between two options (returned j is the option number). + j = i/2; + end \ No newline at end of file diff --git a/matlab/utilities/general/iseven.m b/matlab/utilities/general/iseven.m new file mode 100644 index 000000000..fe15bc3b1 --- /dev/null +++ b/matlab/utilities/general/iseven.m @@ -0,0 +1,20 @@ +function a= iseven(b) + +% Copyright (C) 2014 Dynare Team +% +% This file is part of Dynare. +% +% Dynare is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Dynare. If not, see . + +a = ~mod(b,2); \ No newline at end of file diff --git a/matlab/utilities/general/isodd.m b/matlab/utilities/general/isodd.m new file mode 100644 index 000000000..93f0e0aa0 --- /dev/null +++ b/matlab/utilities/general/isodd.m @@ -0,0 +1,20 @@ +function a = isodd(b) + +% Copyright (C) 2014 Dynare Team +% +% This file is part of Dynare. +% +% Dynare is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Dynare. If not, see . + +a = mod(b,2); \ No newline at end of file