From 74b60028e14aa125d0382b9a5f46bf784548c013 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemia=20=28Scylla=29?= <stepan@dynare.org>
Date: Mon, 10 Dec 2018 22:19:06 +0100
Subject: [PATCH] Closes #21.

It is possible to rename the dseries class. For instance, the
following is a valid syntax:

>> tseries = dseries();
>> tseries(ones(3,2))

ans is a dseries object:

   | Variable_1 | Variable_2
1Y | 1          | 1
2Y | 1          | 1
3Y | 1          | 1

It is also allowed to define a default starting period for the data passed
to the renamed dseries class/object:

>> tseries = dseries('1938M11')
>> tseries(ones(83,2))

ans is a dseries object:

        | Variable_1 | Variable_2
1938M11 | 1          | 1
1938M12 | 1          | 1
1939M1  | 1          | 1
1939M2  | 1          | 1
1939M3  | 1          | 1
1939M4  | 1          | 1
1939M5  | 1          | 1
1939M6  | 1          | 1
1939M7  | 1          | 1
1939M8  | 1          | 1
        |            |
1944M11 | 1          | 1
1944M12 | 1          | 1
1945M1  | 1          | 1
1945M2  | 1          | 1
1945M3  | 1          | 1
1945M4  | 1          | 1
1945M5  | 1          | 1
1945M6  | 1          | 1
1945M7  | 1          | 1
1945M8  | 1          | 1
1945M9  | 1          | 1
---
 src/@dseries/subsref.m | 62 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 58 insertions(+), 4 deletions(-)

diff --git a/src/@dseries/subsref.m b/src/@dseries/subsref.m
index fa9ec6e..9695494 100644
--- a/src/@dseries/subsref.m
+++ b/src/@dseries/subsref.m
@@ -88,7 +88,7 @@ switch S(1).type
       case 'length'
         error(['dseries::subsref: we do not support the length operator on ' ...
                'dseries. Please use ''nobs'' or ''vobs''']);
-      case 'save'                                                        
+      case 'save'
         % Save dseries object on disk (default is a mat file).
         B = NaN;
         if isequal(length(S),2)
@@ -219,6 +219,7 @@ switch S(1).type
             B = A;
         end
     elseif isdates(S(1).subs{1}) || isdate(S(1).subs{1})
+        % Select observation(s) with date(s)
         if isdate(S(1).subs{1})
             Dates = dates(S(1).subs{1});
         else
@@ -236,8 +237,27 @@ switch S(1).type
         B = copy(A);
         B.data = B.data(tdx,:);
         B.dates = B.dates(tdx);
-    elseif isvector(S(1).subs{1}) && all(isint(S(1).subs{1}))
-        error('dseries::subsref: It is not possible to select observations with a vector of integers. You have to index with a dates object instead!');
+    elseif isnumeric(S(1).subs{1}) && isequal(ndims(S(1).subs{1}), 2)
+        if isempty(A)
+            % Populate an empty dseries object.
+            if isempty(A.dates)
+                B = copy(A);
+                B.dates = dates('1Y'):dates('1Y')+(rows(S(1).subs{1})-1);
+                B.data = S(1).subs{1};
+                B.name = default_name(columns(B.data));
+                B.tex = name2tex(B.name);
+                B.ops = cell(length(B.name), 1);
+            else
+                B = copy(A);
+                B.dates = B.dates:B.dates+(rows(S(1).subs{1})-1);
+                B.data = S(1).subs{1};
+                B.name = default_name(columns(B.data));
+                B.tex = name2tex(B.name);
+                B.ops = cell(length(B.name), 1);
+            end
+        else
+            error('dseries::subsref: It is not possible to populate a non empty dseries object!');
+        end
     else
         error('dseries::subsref: I have no idea of what you are trying to do!')
     end
@@ -694,4 +714,38 @@ return
  end
 
  T = all(t);
-%@eof:17
\ No newline at end of file
+%@eof:17
+
+%@test:18
+ try
+     tseries = dseries();
+     ts = tseries(ones(10,1));
+     t(1) = true;
+ catch
+     t(1) = false;
+ end
+
+ if t(1)
+     t(2) = ts.dates(1)==dates('1Y');
+     t(3) = ts.dates(10)==dates('10Y');
+ end
+
+ T = all(t);
+%@eof:18
+
+%@test:19
+ try
+     tseries = dseries(dates('2000Q1'));
+     ts = tseries(ones(5,1));
+     t(1) = true;
+ catch
+     t(1) = false;
+ end
+
+ if t(1)
+     t(2) = ts.dates(1)==dates('2000Q1');
+     t(3) = ts.dates(5)==dates('2001Q1');
+ end
+
+ T = all(t);
+%@eof:19
\ No newline at end of file
-- 
GitLab