diff --git a/src/@dates/subsref.m b/src/@dates/subsref.m index aff73554da462c3545a7c1d03838c2fc49b7eaca..9b4b76b197af439116871231a1742872c39161dd 100644 --- a/src/@dates/subsref.m +++ b/src/@dates/subsref.m @@ -13,7 +13,7 @@ function B = subsref(A,S) % --*-- Unitary tests --*-- % 1. The type of the returned argument depends on the content of S. % 2. See the matlab's documentation about the subsref method. -% Copyright (C) 2011-2020 Dynare Team +% Copyright © 2011-2020 Dynare Team % % This code is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by @@ -126,7 +126,13 @@ switch S(1).type if ~isequal(n1,n2,n3) error('dates::subsref: Second, third and fourth input arguments must have the same number of elements!') end - B.time = [S(1).subs{2}, S(1).subs{3}, S(1).subs{4}]; + % Check that the provided inputs form a valid date + for i=1:length(S(1).subs{1}) + if ~isvalidday([S(1).subs{1}(i), S(1).subs{2}(i), S(1).subs{3}(i)]) + error('Triplet (%u, %u, %u) is not a valid date for a day.', S(1).subs{1}(i), S(1).subs{2}(i), S(1).subs{3}(i)) + end + end + B.time = [datenum([S(1).subs{1}, S(1).subs{2}, S(1).subs{3}]), NaN(length(S(1).subs{1}), 1)]; else error('dates::subsref: Wrong calling sequence!') end @@ -181,7 +187,13 @@ switch S(1).type if ~isequal(n3,n2,n1) error('dates::subsref: All arguments must have the same number of rows!') end - B.time = [S(1).subs{1}, S(1).subs{2}, S(1).subs{3}]; + % Check that the provided inputs form a valid date + for i=1:length(S(1).subs{1}) + if ~isvalidday([S(1).subs{1}(i), S(1).subs{2}(i), S(1).subs{3}(i)]) + error('Triplet (%u, %u, %u) is not a valid date for a day.', S(1).subs{1}(i), S(1).subs{2}(i), S(1).subs{3}(i)) + end + end + B.time = [datenum([S(1).subs{1}, S(1).subs{2}, S(1).subs{3}]), NaN(length(S(1).subs{1}), 1)]; else error('dates::subsref: Wrong number of inputs!') end @@ -207,217 +219,213 @@ if ~isempty(S) B = subsref(B, S); end +return + %@test:1 -%$ % Define a dates object -%$ B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1'); -%$ -%$ % Try to extract a sub-dates object. -%$ d = B(2:3); -%$ -%$ if isa(d,'dates') -%$ t(1) = 1; -%$ else -%$ t(1) = 0; -%$ end -%$ -%$ if t(1) -%$ t(2) = dassert(d.freq,B.freq); -%$ t(3) = dassert(d.time,[1950 2; 1950 3]); -%$ t(4) = dassert(d.ndat(),2); -%$ end -%$ T = all(t); +% Define a dates object +B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1'); + +% Try to extract a sub-dates object. +try + d = B(2:3); + t(1) = true; +catch + t(1) = false; +end + +if t(1) + t(2) = isequal(d.freq, B.freq); + t(3) = isequal(d.time, [1950 2; 1950 3]); + t(4) = isequal(d.ndat(), 2); +end +T = all(t); %@eof:1 %@test:2 -%$ % Define a dates object -%$ B = dates('1950Q1'):dates('1960Q3'); -%$ -%$ % Try to extract a sub-dates object and apply a method -%$ -%$ d = B(2:3).sort ; -%$ -%$ if isa(d,'dates') -%$ t(1) = 1; -%$ else -%$ t(1) = 0; -%$ end -%$ -%$ if t(1) -%$ t(2) = dassert(d.freq,B.freq); -%$ t(3) = dassert(d.time,[1950 2; 1950 3]); -%$ t(4) = dassert(d.ndat(),2); -%$ end -%$ T = all(t); +% Define a dates object +B = dates('1950Q1'):dates('1960Q3'); + +% Try to extract a sub-dates object and apply a method +try + d = B(2:3).sort; + t(1) = true; +catch + t(1) = false; +end + +if t(1) + t(2) = isequal(d.freq,B.freq); + t(3) = isequal(d.time,[1950 2; 1950 3]); + t(4) = isequal(d.ndat(),2); +end +T = all(t); %@eof:2 %@test:3 -%$ % Define a dates object -%$ B = dates('1950Q1'):dates('1960Q3'); -%$ -%$ % Try to extract a sub-dates object and apply a method -%$ -%$ d = B(2:3).sort() ; -%$ -%$ if isa(d,'dates') -%$ t(1) = 1; -%$ else -%$ t(1) = 0; -%$ end -%$ -%$ if t(1) -%$ t(2) = dassert(d.freq,B.freq); -%$ t(3) = dassert(d.time,[1950 2; 1950 3]); -%$ t(4) = dassert(d.ndat(),2); -%$ end -%$ T = all(t); +% Define a dates object +B = dates('1950Q1'):dates('1960Q3'); + +% Try to extract a sub-dates object and apply a method +try + d = B(2:3).sort(); + t(1) = true; +catch + t(1) = false; +end + +if t(1) + t(2) = isequal(d.freq,B.freq); + t(3) = isequal(d.time,[1950 2; 1950 3]); + t(4) = isequal(d.ndat(),2); +end +T = all(t); %@eof:3 %@test:4 -%$ % Define a dates object -%$ B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1'); -%$ -%$ % Try to extract a sub-dates object. -%$ d = B(2); -%$ -%$ if isa(d,'dates') -%$ t(1) = 1; -%$ else -%$ t(1) = 0; -%$ end -%$ -%$ if t(1) -%$ t(2) = dassert(d.freq,B.freq); -%$ t(3) = dassert(d.time,[1950 2]); -%$ t(4) = dassert(d.ndat(),1); -%$ end -%$ T = all(t); +% Define a dates object +B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1'); + +% Try to extract a sub-dates object. +try + d = B(2); + t(1) = true; +catch + t(1) = false; +end + +if t(1) + t(2) = isequal(d.freq,B.freq); + t(3) = isequal(d.time,[1950 2]); + t(4) = isequal(d.ndat(),1); +end +T = all(t); %@eof:4 %@test:5 -%$ % Define an empty dates object with quaterly frequency. -%$ qq = dates('Q'); -%$ -%$ % Define a ranges of dates using qq. -%$ try -%$ r1 = qq(1950,1):qq(1950,3);%qq([1950, 3]); -%$ t(1) = 1; -%$ catch -%$ t(1) = 0; -%$ end -%$ if t(1) -%$ try -%$ r2 = qq([1950, 1; 1950, 2; 1950, 3]); -%$ t(2) = 1; -%$ catch -%$ t(2) = 0; -%$ end -%$ end -%$ if t(1) && t(2) -%$ try -%$ r3 = qq(1950*ones(3,1), transpose(1:3)); -%$ t(3) = 1; -%$ catch -%$ t(3) = 0; -%$ end -%$ end -%$ -%$ if t(1) && t(2) && t(3) -%$ t(4) = dassert(r1,r2); -%$ t(5) = dassert(r1,r3); -%$ end -%$ T = all(t); +% Define an empty dates object with quaterly frequency. +qq = dates('Q'); + +% Define a ranges of dates using qq. +try + r1 = qq(1950,1):qq(1950,3); + t(1) = true; +catch + t(1) = false; +end + +if t(1) + try + r2 = qq([1950, 1; 1950, 2; 1950, 3]); + t(2) = true; + catch + t(2) = false; + end +end +if t(1) && t(2) + try + r3 = qq(1950*ones(3,1), transpose(1:3)); + t(3) = true; + catch + t(3) = false; + end +end + +if t(1) && t(2) && t(3) + t(4) = isequal(r1,r2); + t(5) = isequal(r1,r3); +end +T = all(t); %@eof:5 %@test:6 -%$ % Define an empty dates object with quaterly frequency. -%$ date = dates(); -%$ -%$ % Define a ranges of dates using qq. -%$ try -%$ r1 = date(4,1950,1):date(4,[1950, 3]); -%$ t(1) = 1; -%$ catch -%$ t(1) = 0; -%$ end -%$ if t(1) -%$ try -%$ r2 = date(4,[1950, 1; 1950, 2; 1950, 3]); -%$ t(2) = 1; -%$ catch -%$ t(2) = 0; -%$ end -%$ end -%$ if t(1) && t(2) -%$ try -%$ r3 = date(4,1950*ones(3,1), transpose(1:3)); -%$ t(3) = 1; -%$ catch -%$ t(3) = 0; -%$ end -%$ end -%$ -%$ if t(1) && t(2) && t(3) -%$ t(4) = dassert(r1,r2); -%$ t(5) = dassert(r1,r3); -%$ end -%$ T = all(t); +% Define an empty dates object with quaterly frequency. +date = dates(); + +% Define a ranges of dates using qq. +try + r1 = date(4,1950,1):date(4,[1950, 3]); + t(1) = true; +catch + t(1) = false; +end +if t(1) + try + r2 = date(4,[1950, 1; 1950, 2; 1950, 3]); + t(2) = true; + catch + t(2) = false; + end +end +if t(1) && t(2) + try + r3 = date(4,1950*ones(3,1), transpose(1:3)); + t(3) = true; + catch + t(3) = false; + end +end + +if t(1) && t(2) && t(3) + t(4) = isequal(r1, r2); + t(5) = isequal(r1, r3); +end +T = all(t); %@eof:6 %@test:7 -%$ % Define a dates object -%$ B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1'); -%$ -%$ % Try to extract a sub-dates object. -%$ try -%$ d = B([]); -%$ t(1) = true; -%$ catch -%$ t(1) = false; -%$ end -%$ -%$ if t(1) -%$ t(2) = dassert(isa(d,'dates'), true); -%$ t(3) = dassert(isempty(d), true); -%$ end -%$ T = all(t); +% Define a dates object +B = dates('1950Q1','1950Q2','1950Q3','1950Q4','1951Q1'); + +% Try to extract a sub-dates object. +try + d = B([]); + t(1) = true; +catch + t(1) = false; +end + +if t(1) + t(2) = isequal(isa(d,'dates'), true); + t(3) = isequal(isempty(d), true); +end +T = all(t); %@eof:7 %@test:8 -%$ % Define a dates object -%$ B = dates('1950-11-15','1950-11-16','1950-11-17','1950-11-18'); -%$ -%$ % Try to extract a sub-dates object. -%$ try -%$ d = B([]); -%$ t(1) = true; -%$ catch -%$ t(1) = false; -%$ end -%$ -%$ if t(1) -%$ t(2) = dassert(isa(d,'dates'), true); -%$ t(3) = dassert(isempty(d), true); -%$ end -%$ T = all(t); +% Define a dates object +B = dates('1950-11-15','1950-11-16','1950-11-17','1950-11-18'); + +% Try to extract a sub-dates object. +try + d = B([]); + t(1) = true; +catch + t(1) = false; +end + +if t(1) + t(2) = isequal(isa(d,'dates'), true); + t(3) = isequal(isempty(d), true); +end +T = all(t); %@eof:8 %@test:9 -%$ % Define a dates object -%$ B = dates('1950-11-15','1950-11-16','1950-11-17','1950-11-18'); -%$ -%$ % Try to extract a sub-dates object. -%$ d = B(2); -%$ -%$ if isa(d,'dates') -%$ t(1) = 1; -%$ else -%$ t(1) = 0; -%$ end -%$ -%$ if t(1) -%$ t(2) = dassert(d.freq,B.freq); -%$ t(3) = dassert(d.time,[1950 11 16]); -%$ t(4) = dassert(d.ndat(),1); -%$ end -%$ T = all(t); +% Define a dates object +B = dates('1950-11-15','1950-11-16','1950-11-17','1950-11-18'); + +% Try to extract a sub-dates object. +try + d = B(2); + t(1) = true; +catch + t(1) = false; +end + +if t(1) + t(2) = isequal(d.freq,B.freq); + t(3) = isequal(d.time(1), 712543) && isnan(d.time(2)); + t(4) = isequal(d.ndat(),1); +end +T = all(t); %@eof:9 \ No newline at end of file