1function o = rename_(o, old, new) % --*-- Unitary tests --*-- 2 3% Renames variables in a dseries object. 4% 5% INPUTS 6% - o [dseries] 7% - old [string, cell] 8% - new [string, cell] 9% 10% OUTPUTS 11% - o [dseries] 12 13% Copyright (C) 2013-2017 Dynare Team 14% 15% This file is part of Dynare. 16% 17% Dynare is free software: you can redistribute it and/or modify 18% it under the terms of the GNU General Public License as published by 19% the Free Software Foundation, either version 3 of the License, or 20% (at your option) any later version. 21% 22% Dynare is distributed in the hope that it will be useful, 23% but WITHOUT ANY WARRANTY; without even the implied warranty of 24% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25% GNU General Public License for more details. 26% 27% You should have received a copy of the GNU General Public License 28% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 29 30if isempty(o) 31 error('dseries::rename: Cannot rename variable(s) because the object is empty!') 32end 33 34if nargin<3 35 if isequal(vobs(o), 1) || isequal(vobs(o), length(old)) 36 new = old; 37 else 38 error('dseries::rename: Missing argument!') 39 end 40 if ~ischar(new) && ~(iscellstr(new) && isequal(vobs(o), length(new))) 41 error(['dseries::rename: Input argument ''' ... 42 inputname(2) ... 43 ''' has to be either a string or a cellstring that has the same number of entries as observed variables!']) 44 end 45 idname = 1; 46else 47 if ~ischar(old) || ~ischar(new) 48 error(['dseries::rename: Input arguments ''' inputname(2) ''' and ''' inputname(3) ''' have to be strings!']) 49 end 50 idname = find(strcmp(old,o.name)); 51 if isempty(idname) 52 error(['dseries::rename: Variable ' old ' is unknown in dseries object ' inputname(1) '!']) 53 end 54end 55 56if iscellstr(new) 57 o.name = new(:); 58else 59 o.name(idname) = {new}; 60end 61 62%@test:1 63%$ ts = dseries([transpose(1:5), transpose(6:10)],'1950q1',{'Output'; 'Consumption'}, {'Y_t'; 'C_t'}); 64%$ try 65%$ ts.rename_('Output','Production'); 66%$ t(1) = 1; 67%$ catch 68%$ t(1) = 0; 69%$ end 70%$ 71%$ if t(1)>1 72%$ t(2) = dassert(ts.freq,4); 73%$ t(3) = dassert(ts.init.freq,4); 74%$ t(4) = dassert(ts.init.time,[1950, 1]); 75%$ t(5) = dassert(ts.vobs,2); 76%$ t(6) = dassert(ts.nobs,5); 77%$ t(7) = dassert(ts.name,{'Production'; 'Consumption'}); 78%$ t(8) = dassert(ts.tex,{'Y_t'; 'C_t'}); 79%$ end 80%$ 81%$ T = all(t); 82%@eof:1 83 84%@test:2 85%$ ts = dseries(randn(10,1)); 86%$ try 87%$ ts.rename_('Dora'); 88%$ t(1) = 1; 89%$ catch 90%$ t(1) = 0; 91%$ end 92%$ 93%$ if t(1)>1 94%$ t(2) = dassert(ts.name,{'Dora'}); 95%$ end 96%$ 97%$ T = all(t); 98%@eof:2 99 100%@test:3 101%$ ts = dseries(randn(10,3)); 102%$ try 103%$ ts.rename_({'Dora', 'The', 'Explorer'}); 104%$ t(1) = 1; 105%$ catch 106%$ t(1) = 0; 107%$ end 108%$ 109%$ if t(1) 110%$ t(2) = dassert(ts.name, {'Dora'; 'The'; 'Explorer'}); 111%$ end 112%$ 113%$ T = all(t); 114%@eof:3 115