diff --git a/src/@dseries/remove.m b/src/@dseries/remove.m index 738c9f01e62c1b4a935943cfcc1e0cf324a0c3d6..23ff2e0e92c5deacbc03dd13178d23466357ae43 100644 --- a/src/@dseries/remove.m +++ b/src/@dseries/remove.m @@ -1,6 +1,6 @@ -function o = remove(o, a) +function p = remove(o, a) -% Removes a variable from a dseries object (alias for the pop method). +% Removes a variable from a dseries object (alias for the pop method) or a list of variables. % Copyright © 2014-2023 Dynare Team % @@ -19,4 +19,5 @@ function o = remove(o, a) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <https://www.gnu.org/licenses/>. -o = pop(o, a); +p = copy(o); +p.remove_(a); diff --git a/src/@dseries/remove_.m b/src/@dseries/remove_.m index 50e9c8ace28397614d1da32c937cea677f85d029..cc40402398d84a0802cd8291005353af042e0760 100644 --- a/src/@dseries/remove_.m +++ b/src/@dseries/remove_.m @@ -1,6 +1,6 @@ function o = remove_(o, a) -% Removes a variable from a dseries object (alias for the pop_ method). +% Removes a variable from a dseries object (alias for the pop_ method) or a list of variables. % Copyright © 2017-2023 Dynare Team % @@ -19,4 +19,89 @@ function o = remove_(o, a) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <https://www.gnu.org/licenses/>. -o = pop_(o, a); +if ischar(a) + o = pop_(o, a); +elseif iscellofchar(a) + [isino, io] = ismember(a, o.name); + io = io(isino); + o.data(:,io) = []; + o.name(io) = []; + o.tex(io) = []; + o.ops(io) = []; + otagnames = fieldnames(o.tags); + for i=1:length(otagnames) + o.tags.(otagnames{i})(io) = []; + end + if any(~isino) + w = warning('off', 'backtrace'); + for i=1:length(a) + if ~isino(i) + warning('Variable %s is not a member of the dseries object.', a{i}) + end + end + warning(w.state, 'backtrace') + end +else + error('Unexpected type.') +end + +return % --*-- Unit tests --*-- + +%@test:1 +% Define a datasets. +A = rand(10,6); + +% Define names +A_name = {'A1';'A2';'A3';'A4';'A5';'A6'}; + +% Instantiate a time series object. +try + ts1 = dseries(A,[],A_name,[]); + ts1.tag('type'); + ts1.tag('type', 'A1', 1); + ts1.tag('type', 'A2', 2); + ts1.tag('type', 'A3', 3); + ts1.remove_({'A1','A3','A5'}); + t(1) = true; +catch + t(1) = false; +end + +if t(1) + t(2) = dassert(ts1.vobs,3); + t(3) = dassert(ts1.nobs,10); + t(4) = dassert(ts1.data,[A(:,2), A(:,4), A(:,6)],1e-15); + t(5) = isequal(ts1.name,{'A2';'A4';'A6'}); +end +T = all(t); +%@eof:1 + + +%@test:2 +% Define a datasets. +A = rand(10,6); + +% Define names +A_name = {'A1';'A2';'A3';'A4';'A5';'A6'}; + +% Instantiate a time series object. +try + ts1 = dseries(A,[],A_name,[]); + ts1.tag('type'); + ts1.tag('type', 'A1', 1); + ts1.tag('type', 'A2', 2); + ts1.tag('type', 'A3', 3); + ts1.remove_({'A1','A3','A5','A7','A9'}); + t(1) = true; +catch + t(1) = false; +end + +if t(1) + t(2) = dassert(ts1.vobs,3); + t(3) = dassert(ts1.nobs,10); + t(4) = dassert(ts1.data,[A(:,2), A(:,4), A(:,6)],1e-15); + t(5) = isequal(ts1.name,{'A2';'A4';'A6'}); +end +T = all(t); +%@eof:1