From bbfc1c6e09df75a39e2adda68459d0753c0a1b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Ry=C3=BBk=29?= <stepan@adjemian.eu> Date: Wed, 14 Jun 2023 09:57:52 +0200 Subject: [PATCH] Add optional output. Second output provides the same information but is easier to use (for instance, the second unit test, we use the new ouput to select variables in a dseries object depending on the last observed period). --- src/@dseries/lastobservedperiods.m | 37 +++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/@dseries/lastobservedperiods.m b/src/@dseries/lastobservedperiods.m index 1a45036..aa2a7d5 100644 --- a/src/@dseries/lastobservedperiods.m +++ b/src/@dseries/lastobservedperiods.m @@ -1,4 +1,4 @@ -function b = lastobservedperiods(o) +function [b, D] = lastobservedperiods(o) % Return, for each variable, the last period without missing observations (last period without NaN). % @@ -7,6 +7,7 @@ function b = lastobservedperiods(o) % % OUTPUTS % - b [struct] with N fields, each name field is a variable name and its content a date object. +% - D [dates] N elements. % Copyright © 2023 Dynare Team % @@ -32,6 +33,13 @@ for i=1:o.vobs b.(o.name{i}) = o.dates(find(d(:,i), 1, 'last')); end +if nargout>1 + D = dates(o.dates.freq); + for i=1:o.vobs + D.append_(b.(o.name{i})); + end +end + return % --*-- Unit tests --*-- %@test:1 @@ -55,3 +63,30 @@ end T = all(t); %@eof:1 + +%@test:2 +try + a = randn(10, 3); + a(end,1) = NaN; + a(end,2) = NaN; + a(end-1,2) = NaN; + A = dseries([a, a], '2000Q1', {'A1', 'A2', 'A3', 'B1', 'B2', 'B3'}); + [b, D] = lastobservedperiods(A); + d = unique(D); + tmp = {}; + for i=1:length(d) + tmp{i} = A(D==d(i)); + end + t(1) = true; +catch + t(1) = false; +end + +if t(1) + for i=1:length(d) + t(i+1) = isequal(tmp{i}.name,{sprintf('A%u', i); sprintf('B%u', i)}) + end +end + +T = all(t); +%@eof:2 -- GitLab