subsasgn.m 27 KB
Newer Older
1
function A = subsasgn(A,S,B) % --*-- Unitary tests --*--
2
3
4

%@info:
%! @deftypefn {Function File} {@var{A} =} subsasgn (@var{A}, @var{S}, @var{B})
5
%! @anchor{@dseries/subsasgn}
6
%! @sp 1
7
%! Overloads the subsasgn method for the Dynare time series class (@ref{dseries}).
8
9
10
%! @end deftypefn
%@eod:

11
% Copyright (C) 2012-2014 Dynare Team
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

28
merge_dseries_objects = 1;    
29
30
31
32
33
34

switch length(S)
    case 1
      switch S(1).type
        case '{}' % Multiple variable selection.
          if ~isequal(numel(S(1).subs),numel(unique(S(1).subs)))
35
              error('dseries::subsasgn: Wrong syntax!')
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
          end
          for i=1:numel(S(1).subs)
              element = S(1).subs{i};
              idArobase = strfind(element,'@');
              if ~isempty(idArobase)
                  switch length(idArobase)
                    case 2
                      idComma = strfind(element(idArobase(1)+1:idArobase(2)-1),',');
                      if ~isempty(idComma)
                          elements = cell(1,numel(idComma)+1); j = 1;
                          expression = element(idArobase(1)+1:idArobase(2)-1);
                          while ~isempty(expression)
                              [token, expression] = strtok(expression,',');
                              elements(j) = {[element(1:idArobase(1)-1), token, element(idArobase(2)+1:end)]};
                              j = j + 1;
                          end
                          S(1).subs = replace_object_in_a_one_dimensional_cell_array(S(1).subs, elements(:), i);
                      else
54
                          error('dseries::subsasgn: Wrong syntax, matlab''s regular expressions cannot be used here!')
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
                      end
                    case 4
                      idComma_1 = strfind(element(idArobase(1)+1:idArobase(2)-1),',');
                      idComma_2 = strfind(element(idArobase(3)+1:idArobase(4)-1),',');
                      if ~isempty(idComma_1)
                          elements = cell(1,(numel(idComma_1)+1)*(numel(idComma_2)+1)); j = 1;
                          expression_1 = element(idArobase(1)+1:idArobase(2)-1);
                          while ~isempty(expression_1)
                              [token_1, expression_1] = strtok(expression_1,',');
                              expression_2 = element(idArobase(3)+1:idArobase(4)-1);
                              while ~isempty(expression_2)
                                  [token_2, expression_2] = strtok(expression_2,',');
                                  elements(j) = {[element(1:idArobase(1)-1), token_1, element(idArobase(2)+1:idArobase(3)-1), token_2, element(idArobase(4)+1:end)]};
                                  j = j+1;
                              end
                          end
                          S(1).subs = replace_object_in_a_one_dimensional_cell_array(S(1).subs, elements(:), i);
                      else
73
                          error('dseries::subsasgn: Wrong syntax, matlab''s regular expressions cannot be used here!')
74
75
                      end
                    otherwise
76
                      error('dseries::subsasgn: Wrong syntax!')
77
78
79
                  end
              end
          end
80
81
82
83
84
85
          if isempty(B)
              for i=1:length(S(1).subs)
                  A = remove(A,S(1).subs{i});
              end
              return
          end
86
          if ~isequal(length(S(1).subs),B.vobs)
87
              error('dseries::subsasgn: Wrong syntax!')
88
89
90
91
92
          end
          if ~isequal(S(1).subs(:),B.name)
              for i = 1:B.vobs
                  if ~isequal(S(1).subs{i},B.name{i})
                      % Rename a variable.
93
                      id = find(strcmp(S(1).subs{i},A.name));
94
95
96
97
98
99
100
101
102
103
104
105
                      if isempty(id)
                          % Add a new variable a change its name.
                          B.name(i) = {S(1).subs{i}};
                          B.tex(i) = {name2tex(S(1).subs{i})};
                      else
                          % Rename variable and change its content.
                          B.name(i) = A.name(id);
                          B.tex(i) = A.tex(id);
                      end
                  end
              end
          end
106
        case '.'
107
          if isequal(S(1).subs,'init') && isdates(B) && isequal(length(B),1)
108
109
110
111
              % Overwrite the init member...
              A.init = B;
              % ... and update freq and time members.
              A.freq = A.init.freq;
112
              A.dates = A.init:A.init+(A.nobs-1);
113
              return
114
          elseif isequal(S(1).subs,'dates') && isdates(B)
115
              % Overwrite the time member...
116
              A.dates = B;
117
118
119
120
              % ... and update the freq and init members.
              A.init = B(1);
              A.freq = A.init.freq;
              return
121
          elseif ismember(S(1).subs,{'freq','nobs','vobs','data','name','tex'})
122
              error(['dseries::subsasgn: You cannot overwrite ' S(1).subs ' member!'])
123
124
          elseif ~isequal(S(1).subs,B.name)
              % Single variable selection.
125
126
              if ~isequal(S(1).subs,B.name{1})
                  % Rename a variable.
127
                  id = find(strcmp(S(1).subs,A.name));
128
129
130
131
132
133
134
135
136
137
138
139
                  if isempty(id)
                      % Add a new variable a change its name.
                      B.name(1) = {S(1).subs};
                      B.tex(1) = {name2tex(S(1).subs)};
                  else
                      % Rename variable and change its content.
                      B.name(1) = A.name(id);
                      B.tex(1) = A.tex(id);
                  end
              end
          end
        case '()' % Date(s) selection
140
141
142
143
144
145
146
          if isdates(S(1).subs{1}) || isdate(S(1).subs{1})
                if isdate(S(1).subs{1})
                    Dates = dates(S(1).subs{1});
                else
                    Dates = S(1).subs{1};
                end
              [junk, tdx] = intersect(A.dates.time,Dates.time,'rows');
147
              if isdseries(B)
148
                  [junk, tdy] = intersect(B.dates.time,Dates.time,'rows');
149
                  if isempty(tdy)
150
                      error('dseries::subsasgn: Periods of the dseries objects on the left and right hand sides must intersect!')
151
152
                  end
                  if ~isequal(A.vobs,B.vobs)
153
                      error('dseries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
154
155
                  end
                  A.data(tdx,:) = B.data(tdy,:);
156
                  merge_dseries_objects = 0;
157
              elseif isnumeric(B)
158
                  merge_dseries_objects = 0;
159
160
161
162
                  if isequal(length(tdx),rows(B))
                      if isequal(columns(A.data),columns(B))
                          A.data(tdx,:) = B;
                      else
163
                          error('dseries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
164
165
                      end
                  else
166
                      error('dseries::subsassgn: Dimension error! The number of periods on the left and right hand side must match.')
167
168
                  end
              else
169
                  error('dseries::subsasgn: The object on the right hand side must be a dseries object or a numeric array!')
170
              end
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
          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
206
          else
207
              error('dseries::subsasgn: Wrong syntax!')
208
209
          end
        otherwise
210
          error('dseries::subsasgn: Wrong syntax!')
211
212
      end
  case 2
213
    merge_dseries_objects = 0;
214
    if ((isequal(S(1).type,'{}') || isequal(S(1).type,'.')) && isequal(S(2).type,'()'))
215
216
217
218
219
        if isequal(S(1).type,'{}')
            sA = extract(A,S(1).subs{:});
        else
            sA = extract(A,S(1).subs);
        end
220
221
222
223
224
        if (isdseries(B) && isequal(sA.vobs,B.vobs)) || (isnumeric(B) && isequal(sA.vobs,columns(B))) || (isnumeric(B) && isequal(columns(B),1)) 
            if isdates(S(2).subs{1})
                [junk, tdx] = intersect(sA.dates.time,S(2).subs{1}.time,'rows');
                if isdseries(B)
                    [junk, tdy] = intersect(B.dates.time,S(2).subs{1}.time,'rows');
225
                    if isempty(tdy)
226
                        error('dseries::subsasgn: Periods of the dseries objects on the left and right hand sides must intersect!')
227
                    end
228
229
                    sA.data(tdx,:) = B.data(tdy,:);
                elseif isnumeric(B)
230
                    merge_dseries_objects = 0;
231
232
233
                    if isequal(length(tdx),rows(B))
                        if isequal(columns(sA.data),columns(B))
                            sA.data(tdx,:) = B;
234
235
                        elseif isequal(size(B,2),1)
                            sA.data(tdx,:) = repmat(B,1,columns(sA.data));
236
                        else
237
                            error('dseries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
238
                        end
239
                    else
240
241
242
                        if isequal(columns(sA.data),columns(B)) && isequal(rows(B),1)
                            sA.data(tdx,:) = repmat(B,length(tdx),1);
                        elseif isequal(rows(B),1)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
243
                            sA.data(tdx,:) = B;
244
                        else
245
                            error('dseries::subsassgn: Dimension error! The number of periods on the left and right hand side must match.')
246
                        end
247
248
                    end
                else
249
                    error('dseries::subsasgn: The object on the right hand side must be a dseries object or a numeric array!')
Stéphane Adjemian's avatar
Stéphane Adjemian committed
250
                end
251
            else
252
                error('dseries::subsasgn: Wrong syntax!')
Stéphane Adjemian's avatar
Stéphane Adjemian committed
253
            end
254
255
            A = merge(A,sA);
        else
256
            error('dseries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
Stéphane Adjemian's avatar
Stéphane Adjemian committed
257
258
        end
    end
259
  otherwise
260
    error('dseries::subsasgn: Wrong syntax!')
261
end
Stéphane Adjemian's avatar
Stéphane Adjemian committed
262

263
264
265
266
267
268
if isempty(A)
    % Assign variables to an empty dseries object.
    A = B;
    return
end

269
if merge_dseries_objects
270
    A = merge(A,B);
271
272
end

273
274
275
276
%@test:1
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,1);
%$
277
278
279
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'B1'},[]);
280
281
%$
%$ % modify first object.
282
283
284
285
286
287
288
%$ try
%$     ts1{'A2'} = ts2;
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end 
%$ 
289
%$ % Instantiate a time series object.
290
%$ if t(1)
291
292
%$    t(2) = dyn_assert(ts1.vobs,3);
%$    t(3) = dyn_assert(ts1.nobs,10);
293
%$    t(4) = dyn_assert(ts1.name{2},'A2');
294
295
296
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.data,[A(:,1), B, A(:,3)],1e-15);
297
%$ end
298
299
300
301
302
303
304
%$ T = all(t);
%@eof:1

%@test:2
%$ % Define a datasets.
%$ A = rand(10,3);
%$
305
306
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
%$
%$ % Apply the exponential function to the second variable.
%$ ts1{'A2'} = ts1{'A2'}.exp;
%$
%$ % Instantiate a time series object.
%$
%$    t(1) = dyn_assert(ts1.vobs,3);
%$    t(2) = dyn_assert(ts1.nobs,10);
%$    t(3) = dyn_assert(ts1.name{2},'A2');
%$    t(4) = dyn_assert(ts1.name{1},'A1');
%$    t(5) = dyn_assert(ts1.name{3},'A3');
%$    t(6) = dyn_assert(ts1.data,[A(:,1), exp(A(:,2)), A(:,3)],1e-15);
%$ T = all(t);
%@eof:2

%@test:3
%$ % Define a datasets.
%$ A = rand(10,3);
%$
326
327
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
%$
%$ % Apply the logarithm function to the first and third variables.
%$ ts1{'A1'} = ts1{'A1'}.log;
%$ ts1{'A3'} = ts1{'A3'}.log;
%$
%$ % Instantiate a time series object.
%$
%$    t(1) = dyn_assert(ts1.vobs,3);
%$    t(2) = dyn_assert(ts1.nobs,10);
%$    t(3) = dyn_assert(ts1.name{2},'A2');
%$    t(4) = dyn_assert(ts1.name{1},'A1');
%$    t(5) = dyn_assert(ts1.name{3},'A3');
%$    t(6) = dyn_assert(ts1.data,[log(A(:,1)), A(:,2), log(A(:,3))],1e-15);
%$ T = all(t);
%@eof:3
343
344
345

%@test:4
%$ % Define a datasets.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
346
%$ A = rand(10,3);
347
%$
348
349
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
350
%$
Stéphane Adjemian's avatar
Stéphane Adjemian committed
351
352
%$ % Apply the logarithm function to the first and third variables of ts1.
%$ ts1{'A1','A3'} = ts1{'A1','A3'}.log;
353
%$
Stéphane Adjemian's avatar
Stéphane Adjemian committed
354
%$ t(1) = dyn_assert(ts1.vobs,3);
355
%$ t(2) = dyn_assert(ts1.nobs,10);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
356
357
%$ t(3) = dyn_assert(ts1.name{1},'A1') && dyn_assert(ts1.name{2},'A2') && dyn_assert(ts1.name{3},'A3');
%$ t(4) = dyn_assert(ts1.data,[log(A(:,1)), A(:,2), log(A(:,3))],1e-15);
358
359
360
361
362
363
364
%$ T = all(t);
%@eof:4

%@test:5
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,3);
%$
365
366
367
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'A1';'B2';'B3'},[]);
368
369
370
371
372
373
374
375
376
377
378
379
380
381
%$
%$ % Apply the logarithm function to the first and third variables.
%$ ts1.A1 = ts2.A1;
%$
%$ % Instantiate a time series object.
%$
%$ t(1) = dyn_assert(ts1.vobs,3);
%$ t(2) = dyn_assert(ts1.nobs,10);
%$ t(3) = dyn_assert(ts1.name{1},'A1');
%$ t(4) = dyn_assert(ts1.data(:,1),B(:,1), 1e-15);
%$ t(5) = dyn_assert(ts1.data(:,2:3),A(:,2:3), 1e-15);
%$ T = all(t);
%@eof:5

382
383
384
385
%@test:6
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,2);
%$
386
387
388
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'B1';'B2'},[]);
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
%$
%$ % Call tested routine.
%$ try
%$     ts1.B2 = ts2.B2.log;
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,4);
%$    t(3) = dyn_assert(ts1.nobs,10);
%$    t(4) = dyn_assert(ts1.name{1},'A1');
%$    t(5) = dyn_assert(ts1.name{2},'A2');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.name{4},'B2');
%$    t(8) = dyn_assert(ts1.data,[A(:,1), A(:,2), A(:,3), log(B(:,2))],1e-15);
%$ end
%$ T = all(t);
%@eof:6

%@test:7
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,2);
%$
415
416
417
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'B1';'B2'},[]);
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
%$
%$ % Append B2 to the first object.
%$ ts1{'B2'} = ts2{'B2'};
%$ t(1) = 1;
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,4);
%$    t(3) = dyn_assert(ts1.nobs,10);
%$    t(4) = dyn_assert(ts1.name{1},'A1');
%$    t(5) = dyn_assert(ts1.name{2},'A2');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(6) = dyn_assert(ts1.name{4},'B2');
%$    t(7) = dyn_assert(ts1.data,[A(:,1), A(:,2), A(:,3), B(:,2)],1e-15);
%$ end
%$ T = all(t);
433
%@eof:7
434

435
%@test:8
436
437
438
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,1);
%$
439
440
441
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'B1'},[]);
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
%$
%$ % modify first object.
%$ try
%$     ts1{'A4'} = ts2;
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end 
%$ 
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,4);
%$    t(3) = dyn_assert(ts1.nobs,10);
%$    t(4) = dyn_assert(ts1.name{2},'A2');
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.name{4},'A4');
%$    t(8) = dyn_assert(ts1.data,[A, B],1e-15);
%$ end
%$ T = all(t);
462
%@eof:8
463

464
%@test:9
465
466
467
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,2);
%$
468
469
470
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'A1';'B1'},[]);
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
%$
%$ % modify first object.
%$ try
%$     ts1{'A1','A4'} = ts2;
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end 
%$ 
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,4);
%$    t(3) = dyn_assert(ts1.nobs,10);
%$    t(4) = dyn_assert(ts1.name{2},'A2');
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.name{4},'A4');
%$    t(8) = dyn_assert(ts1.data,[B(:,1), A(:,2:3), B(:,2)],1e-15);
%$ end
%$ T = all(t);
491
492
493
494
495
496
497
%@eof:9


%@test:10
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,3);
%$
498
499
500
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'A1';'B1';'B2'},[]);
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
%$
%$ % modify first object.
%$ try
%$     ts1{'A@1,2@','A4'} = ts2;
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end 
%$ 
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,4);
%$    t(3) = dyn_assert(ts1.nobs,10);
%$    t(4) = dyn_assert(ts1.name{1},'A1');
%$    t(5) = dyn_assert(ts1.name{2},'A2');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.name{4},'A4');
%$    t(8) = dyn_assert(ts1.data,[B(:,1:2), A(:,3), B(:,3)],1e-15);
%$ end
%$ T = all(t);
%@eof:10
522

523
%@test:11
524
525
526
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,5);
%$
527
528
529
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A_1';'A_2';'A_3'},[]);
%$ ts2 = dseries(B,[],{'A_1';'A_2';'B_1';'B_2';'B_3'},[]);
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
%$
%$ % modify first object.
%$ try
%$     ts1{'@A,B@_@1,2@'} = ts2{'@A,B@_@1,2@'};
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    %t(2) = dyn_assert(ts1.vobs,4);
%$    %t(3) = dyn_assert(ts1.nobs,10);
%$    %t(4) = dyn_assert(ts1.name,{'A1','A2';'A3';'B1';'B2'});
%$    %t(5) = dyn_assert(ts1.data,[B(:,1:2), A(:,3), B(:,3:4)],1e-15);
%$ end
%$ T = all(t);
547
%@eof:11
548
549
550
551
552

%@test:12
%$ % Define a datasets.
%$ A = rand(40,3); B = rand(40,1);
%$
553
554
555
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
556
557
558
%$
%$ % modify first object.
%$ try
559
560
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
561
562
563
564
565
566
567
568
569
570
571
572
573
574
%$     rg = d1:d2;
%$     ts1{'A1'}(rg) = ts2{'B1'}(rg);
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,3);
%$    t(3) = dyn_assert(ts1.nobs,40);
%$    t(4) = dyn_assert(ts1.name{2},'A2');
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
575
%$    t(7) = dyn_assert(ts1.data,[[A(1:2,1); B(3:7); A(8:end,1)], A(:,2:3)],1e-15);
576
577
578
%$ end
%$ T = all(t);
%@eof:12
579
580
581
582
583

%@test:13
%$ % Define a datasets.
%$ A = rand(40,3); B = rand(40,1);
%$
584
585
586
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
587
588
589
%$
%$ % modify first object.
%$ try
590
591
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
%$     rg = d1:d2;
%$     ts1{'A1'}(rg) = B(3:7);
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,3);
%$    t(3) = dyn_assert(ts1.nobs,40);
%$    t(4) = dyn_assert(ts1.name{2},'A2');
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.data,[[A(1:2,1); B(3:7); A(8:end,1)], A(:,2:3)],1e-15);
%$ end
%$ T = all(t);
609
610
611
612
613
614
%@eof:13

%@test:14
%$ % Define a datasets.
%$ A = rand(40,3); B = rand(40,1);
%$
615
616
617
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
618
619
620
%$
%$ % modify first object.
%$ try
621
622
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
%$     rg = d1:d2;
%$     ts1.A1(rg) = B(3:7);
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,3);
%$    t(3) = dyn_assert(ts1.nobs,40);
%$    t(4) = dyn_assert(ts1.name{2},'A2');
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.data,[[A(1:2,1); B(3:7); A(8:end,1)], A(:,2:3)],1e-15);
%$ end
%$ T = all(t);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
640
641
642
643
644
645
%@eof:14

%@test:15
%$ % Define a datasets.
%$ A = rand(40,3); B = rand(40,1);
%$
646
647
648
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
649
650
651
%$
%$ % modify first object.
%$ try
652
653
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
%$     rg = d1:d2;
%$     ts1.A1(rg) = sqrt(pi);
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,3);
%$    t(3) = dyn_assert(ts1.nobs,40);
%$    t(4) = dyn_assert(ts1.name{2},'A2');
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.data,[[A(1:2,1); repmat(sqrt(pi),5,1); A(8:end,1)], A(:,2:3)],1e-15);
%$ end
%$ T = all(t);
%@eof:15

%@test:16
%$ % Define a datasets.
%$ A = rand(40,3); B = rand(40,1);
%$
677
678
679
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
680
681
682
%$
%$ % modify first object.
%$ try
683
684
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
%$     rg = d1:d2;
%$     ts1{'A1','A2'}(rg) = sqrt(pi);
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,3);
%$    t(3) = dyn_assert(ts1.nobs,40);
%$    t(4) = dyn_assert(ts1.name{2},'A2');
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.data,[[A(1:2,1); repmat(sqrt(pi),5,1); A(8:end,1)], [A(1:2,2); repmat(sqrt(pi),5,1); A(8:end,2)], A(:,3)],1e-15);
%$ end
%$ T = all(t);
%@eof:16

%@test:17
%$ % Define a datasets.
%$ A = rand(40,3); B = rand(40,1);
%$
708
709
710
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
711
712
713
%$
%$ % modify first object.
%$ try
714
715
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
%$     rg = d1:d2;
%$     ts1{'A1','A2'}(rg) = [sqrt(pi), pi];
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,3);
%$    t(3) = dyn_assert(ts1.nobs,40);
%$    t(4) = dyn_assert(ts1.name{2},'A2');
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.data,[[A(1:2,1); repmat(sqrt(pi),5,1); A(8:end,1)], [A(1:2,2); repmat(pi,5,1); A(8:end,2)], A(:,3)],1e-15);
%$ end
%$ T = all(t);
%@eof:17

%@test:18
%$ % Define a datasets.
%$ A = rand(40,3); B = rand(40,1);
%$
739
740
741
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
742
743
744
%$
%$ % modify first object.
%$ try
745
746
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
%$     rg = d1:d2;
%$     ts1{'A1','A2'}(rg) = ones(5,1);
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,3);
%$    t(3) = dyn_assert(ts1.nobs,40);
%$    t(4) = dyn_assert(ts1.name{2},'A2');
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.data,[[A(1:2,1); ones(5,1); A(8:end,1)], [A(1:2,2); ones(5,1); A(8:end,2)], A(:,3)],1e-15);
%$ end
%$ T = all(t);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
764
765
766
767
768
769
%@eof:18

%@test:19
%$ % Define a datasets.
%$ A = rand(40,3);
%$
770
771
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
772
%$
773
774
%$ % Instantiate a dates object.
%$ dd = dates('1952Q1');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
%$
%$ % modify first object.
%$ try
%$     ts1.init = dd;
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,3);
%$    t(3) = dyn_assert(ts1.nobs,40);
%$    t(4) = dyn_assert(ts1.name{2},'A2');
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.data,A,1e-15);
%$    t(8) = dyn_assert(isequal(ts1.init,dd),1);
793
%$    t(9) = dyn_assert(isequal(ts1.dates(1),dd),1);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
794
795
796
797
798
799
800
801
%$ end
%$ T = all(t);
%@eof:19

%@test:20
%$ % Define a datasets.
%$ A = rand(40,3);
%$
802
803
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
804
%$
805
806
%$ % Instantiate a dates object.
%$ dd = dates('1952Q1');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
807
808
809
%$
%$ % modify first object.
%$ try
810
%$     ts1.dates = dd:(dd+(ts1.nobs-1));
Stéphane Adjemian's avatar
Stéphane Adjemian committed
811
812
813
814
815
816
817
818
819
820
821
822
823
824
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dyn_assert(ts1.vobs,3);
%$    t(3) = dyn_assert(ts1.nobs,40);
%$    t(4) = dyn_assert(ts1.name{2},'A2');
%$    t(5) = dyn_assert(ts1.name{1},'A1');
%$    t(6) = dyn_assert(ts1.name{3},'A3');
%$    t(7) = dyn_assert(ts1.data,A,1e-15);
%$    t(8) = dyn_assert(isequal(ts1.init,dd),1);
825
%$    t(9) = dyn_assert(isequal(ts1.dates(1),dd),1);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
826
827
%$ end
%$ T = all(t);
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
%@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

854
%@test:22
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
%$ % 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);
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
%@eof:22

%@test:23
%$ % Instantiate a dseries object.
%$ ts0 = dseries(randn(10,6), '1999y');
%$
%$ % Try to remove Variable_2 and Variable_3
%$ try
%$     ts0{'Variable_@2,3@'} = [];
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ if t(1)
%$    % Try to display Variable_2 and Variable_3 again (should fail because already removed)
%$    try
%$       ts0{'Variable_@2,3@'};
%$       t(2) = 0;
%$    catch
%$       t(2) = 1;
%$    end
%$ end
%$ T = all(t);
%@eof:23