From 20041ac70c5746c3c261ebf996417789cf4bd39e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Scylla=29?=
 <stephane.adjemian@univ-lemans.fr>
Date: Sat, 22 Mar 2014 12:07:29 +0100
Subject: [PATCH] New syntax for populating an empty dseries object.

If ts is an empty dseries object with a defined range of dates:

ts = dseries(dates('1990Q1'):dates('1990Q4'));

Then the following syntaxes are valid:

1. ts(:) = 1;
2. ts(:) = [1, 2];
3. ts(:) = randn(4,1);
4. ts(:) = dseries(1);
5. ts(:) = dseries([1, 2]);
6. ts(:) = dseries(randn(4,2));
7. ts(:) = dseries(randn(4,2),dates('1950M1'):dates('1950M4'));

Remarks.

[1] In cases 1., 2., 4. and 5. the single observation is replicated to match the number of dates in ts.

[2] In cases 4. to 7. the dates in the right member of the assignment are lost (ie ts.dates is not affected by the dates defined in the right members).
---
 matlab/@dseries/subsasgn.m | 85 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 84 insertions(+), 1 deletion(-)

diff --git a/matlab/@dseries/subsasgn.m b/matlab/@dseries/subsasgn.m
index 4874e2017d..17fc472cde 100644
--- a/matlab/@dseries/subsasgn.m
+++ b/matlab/@dseries/subsasgn.m
@@ -162,6 +162,41 @@ switch length(S)
               else
                   error('dseries::subsasgn: The object on the right hand side must be a dseries object or a numeric array!')
               end
+          elseif ischar(S(1).subs{1}) && isequal(S(1).subs{1},':') && isempty(A)
+              if isnumeric(B)
+                  if isequal(rows(B),1)
+                      A.data = repmat(B,A.dates.ndat,1);
+                      A.nobs = rows(A.data);
+                      A.vobs = columns(A.data);
+                  elseif isequal(rows(B),A.dates.ndat)
+                      A.data = B;
+                      A.nobs = rows(A.data);
+                      A.vobs = columns(A.data);
+                  else
+                      error('dseries::subsasgn: Wrong syntax!')
+                  end
+                  if isempty(A.name)
+                      A.name = default_name(A.vobs);
+                      A.tex = name2tex(A.name);
+                  end
+              elseif isdseries(B)
+                  if isequal(B.nobs,1)
+                      A.data = repmat(B.data,A.dates.ndat,1);
+                      A.nobs = rows(A.data);
+                      A.vobs = columns(A.data);
+                  elseif isequal(B.nobs,A.dates.ndat)
+                      A.data = B;
+                      A.nobs = rows(A.data);
+                      A.vobs = columns(A.data);
+                  else
+                      error('dseries::subsasgn: Wrong syntax!')
+                  end
+                  if isempty(A.name)
+                      A.name = B.name;
+                      A.tex = B.tex;
+                  end
+              end
+              return
           else
               error('dseries::subsasgn: Wrong syntax!')
           end
@@ -784,4 +819,52 @@ end
 %$    t(9) = dyn_assert(isequal(ts1.dates(1),dd),1);
 %$ end
 %$ T = all(t);
-%@eof:20
\ No newline at end of file
+%@eof:20
+
+%@test:21
+%$ % Define a datasets.
+%$ A = rand(4,3);
+%$
+%$ % Instantiate an empty dseries object.
+%$ ts = dseries(dates('1950Q1'):dates('1950Q4'));
+%$
+%$ % Populate ts
+%$ try
+%$     ts(:) = A;
+%$     t(1) = 1;
+%$ catch
+%$     t(1) = 0;
+%$ end
+%$
+%$ % Instantiate a time series object.
+%$ if t(1)
+%$    t(2) = dyn_assert(ts.vobs,3);
+%$    t(3) = dyn_assert(ts.nobs,4);
+%$    t(4) = dyn_assert(ts.data,A,1e-15);
+%$ end
+%$ T = all(t);
+%@eof:21
+
+%@test:21
+%$ % Define a datasets.
+%$ A = rand(1,3);
+%$
+%$ % Instantiate an empty dseries object.
+%$ ts = dseries(dates('1950Q1'):dates('1950Q4'));
+%$
+%$ % Populate ts
+%$ try
+%$     ts(:) = A;
+%$     t(1) = 1;
+%$ catch
+%$     t(1) = 0;
+%$ end
+%$
+%$ % Instantiate a time series object.
+%$ if t(1)
+%$    t(2) = dyn_assert(ts.vobs,3);
+%$    t(3) = dyn_assert(ts.nobs,4);
+%$    t(4) = dyn_assert(ts.data,repmat(A,4,1),1e-15);
+%$ end
+%$ T = all(t);
+%@eof:21
\ No newline at end of file
-- 
GitLab