subsasgn.m 23.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
function A = subsasgn(A,S,B) % --*-- Unitary tests --*--

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

11
% Copyright © 2012-2020 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 = true;
29 30

switch length(S)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  case 1
    switch S(1).type
      case '{}' % Multiple variable selection.
        if ~isequal(numel(S(1).subs),numel(unique(S(1).subs)))
            error('dseries::subsasgn: Wrong syntax!')
        end
        for i=1:numel(S(1).subs)
            element = S(1).subs{i};
            % Implicit loop.
            idArobase = strfind(element,'@');
            if mod(length(idArobase),2)
                error('dseries::subsasgn: (Implicit loops) The number of @ symbols must be even!')
            end
            % regular expression.
            idBracket.open = strfind(element, '[');
            idBracket.close = strfind(element, ']');
            if ~isequal(length(idBracket.open),length(idBracket.open))
Houtan Bastani's avatar
Houtan Bastani committed
48
                error('dseries::subsasgn: (MATLAB/Octave''s regular expressions) Check opening and closing square brackets!')
Stéphane Adjemian's avatar
Stéphane Adjemian committed
49 50
            end
            % Loops and regular expressions are not compatible
51 52
            if ~isempty(idArobase) && ~isempty(idBracket.open)
                error('dseries::subsasgn: You cannot use implicit loops and regular expressions in the same rule!')
Stéphane Adjemian's avatar
Stéphane Adjemian committed
53 54 55 56 57 58
            end
            if ~isempty(idArobase)
                elements = build_list_of_variables_with_loops({}, idArobase, element, {});
                S(1).subs = replace_object_in_a_one_dimensional_cell_array(S(1).subs, elements(:), i);
            end
            if ~isempty(idBracket.open)
59
                elements = build_list_of_variables_with_regexp(A.name, element(2:end-1), false);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
                S(1).subs = replace_object_in_a_one_dimensional_cell_array(S(1).subs, elements(:), i);
            end
        end
        if isempty(B)
            for i=1:length(S(1).subs)
                A = remove(A,S(1).subs{i});
            end
            return
        end
        if ~isequal(length(S(1).subs),vobs(B))
            error('dseries::subsasgn: Wrong syntax!')
        end
        if ~isequal(S(1).subs(:),B.name)
            for i = 1:vobs(B)
                if ~isequal(S(1).subs{i},B.name{i})
                    % Rename a variable.
                    id = find(strcmp(S(1).subs{i},A.name));
                    if isempty(id)
                        % Add a new variable a change its name.
79
                        B.name(i) = S(1).subs(i);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
                        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
      case '.'
        if isequal(S(1).subs,'init') && isdates(B) && isequal(length(B),1)
            % Change the initial date (update dates member)
            A.dates = B:B+(nobs(A)-1);
            return
        elseif isequal(S(1).subs,'dates') && isdates(B)
            % Overwrite the dates member
            A.dates = B;
            return
Stéphane Adjemian's avatar
Stéphane Adjemian committed
98
        elseif ismember(S(1).subs,{'data','name','tex','ops'})
Stéphane Adjemian's avatar
Stéphane Adjemian committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            error(['dseries::subsasgn: You cannot overwrite ' S(1).subs ' member!'])
        elseif ~isequal(S(1).subs,B.name)
            % Single variable selection.
            if ~isequal(S(1).subs,B.name{1})
                % Rename a variable.
                id = find(strcmp(S(1).subs,A.name));
                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
        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
123
            [~, tdx] = intersect(A.dates.time,Dates.time,'rows');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
124
            if isdseries(B)
125
                [~, tdy] = intersect(B.dates.time,Dates.time,'rows');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
126 127 128 129 130 131 132
                if isempty(tdy)
                    error('dseries::subsasgn: Periods of the dseries objects on the left and right hand sides must intersect!')
                end
                if ~isequal(vobs(A), vobs(B))
                    error('dseries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
                end
                A.data(tdx,:) = B.data(tdy,:);
133
                merge_dseries_objects = false;
Stéphane Adjemian's avatar
Stéphane Adjemian committed
134
            elseif isnumeric(B)
135
                merge_dseries_objects = false;
Stéphane Adjemian's avatar
Stéphane Adjemian committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
                if isequal(length(tdx),rows(B))
                    if isequal(columns(A.data),columns(B))
                        A.data(tdx,:) = B;
                    else
                        error('dseries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
                    end
                else
                    error('dseries::subsassgn: Dimension error! The number of periods on the left and right hand side must match.')
                end
            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)
                A.data = B;
                A.name = default_name(vobs(A));
                A.tex = name2tex(A.name);
                if isempty(A.dates)
                    if isempty(A.dates.freq)
155
                        init = dates('1Y');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
156 157 158
                    else
                        init = dates(A.dates.freq, 1, 1);
                    end
159
                else
Stéphane Adjemian's avatar
Stéphane Adjemian committed
160
                    init = A.dates(1);
161
                end
Stéphane Adjemian's avatar
Stéphane Adjemian committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175
                A.dates = init:(init+rows(B)-1);
            elseif isdseries(B)
                A.data = B.data;
                A.name = B.name;
                A.tex = N.tex;
                A.dates = B.dates;
            end
            return
        else
            error('dseries::subsasgn: Wrong syntax!')
        end
      otherwise
        error('dseries::subsasgn: Wrong syntax!')
    end
176
  case 2
177
    merge_dseries_objects = false;
178 179 180 181 182 183 184 185
    if ((isequal(S(1).type,'{}') || isequal(S(1).type,'.')) && isequal(S(2).type,'()'))
        if isequal(S(1).type,'{}')
            sA = extract(A,S(1).subs{:});
        else
            sA = extract(A,S(1).subs);
        end
        if (isdseries(B) && isequal(vobs(sA), vobs(B))) || (isnumeric(B) && isequal(vobs(sA),columns(B))) || (isnumeric(B) && isequal(columns(B),1))
            if isdates(S(2).subs{1})
186
                [~, tdx] = intersect(sA.dates.time,S(2).subs{1}.time,'rows');
187
                if isdseries(B)
188
                    [~, tdy] = intersect(B.dates.time,S(2).subs{1}.time,'rows');
189 190 191 192 193
                    if isempty(tdy)
                        error('dseries::subsasgn: Periods of the dseries objects on the left and right hand sides must intersect!')
                    end
                    sA.data(tdx,:) = B.data(tdy,:);
                elseif isnumeric(B)
194
                    merge_dseries_objects = false;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
                    if isequal(length(tdx),rows(B))
                        if isequal(columns(sA.data),columns(B))
                            sA.data(tdx,:) = B;
                        elseif isequal(size(B,2),1)
                            sA.data(tdx,:) = repmat(B,1,columns(sA.data));
                        else
                            error('dseries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
                        end
                    else
                        if isequal(columns(sA.data),columns(B)) && isequal(rows(B),1)
                            sA.data(tdx,:) = repmat(B,length(tdx),1);
                        elseif isequal(rows(B),1)
                            sA.data(tdx,:) = B;
                        else
                            error('dseries::subsassgn: Dimension error! The number of periods on the left and right hand side must match.')
                        end
                    end
                else
                    error('dseries::subsasgn: The object on the right hand side must be a dseries object or a numeric array!')
                end
            else
                error('dseries::subsasgn: Wrong syntax!')
            end
218
            A = merge(A, sA, true);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        else
            error('dseries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
        end
    end
  otherwise
    error('dseries::subsasgn: Wrong syntax!')
end

if isempty(A)
    % Assign variables to an empty dseries object.
    A = B;
    return
end

if merge_dseries_objects
234
    A = merge(A, B, true);
235 236 237 238 239 240 241 242 243 244 245 246
end

%@test:1
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,1);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'B1'},[]);
%$
%$ % modify first object.
%$ try
247
%$     ts1{'A2'} = ts2;
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dassert(ts1.vobs,3);
%$    t(3) = dassert(ts1.nobs,10);
%$    t(4) = dassert(ts1.name{2},'A2');
%$    t(5) = dassert(ts1.name{1},'A1');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(ts1.data,[A(:,1), B, A(:,3)],1e-15);
%$ end
%$ T = all(t);
%@eof:1

%@test:2
%$ % Define a datasets.
%$ A = rand(10,3);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$
272 273
%$ try
%$     % Apply the exponential function to the second variable.
274
%$     ts1{'A2'} = ts1{'A2'}.exp;
275 276 277 278
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
279
%$
280 281 282 283 284 285 286 287
%$ if t(1)
%$     t(2) = dassert(ts1.vobs,3);
%$     t(3) = dassert(ts1.nobs,10);
%$     t(4) = dassert(ts1.name{2},'A2');
%$     t(5) = dassert(ts1.name{1},'A1');
%$     t(6) = dassert(ts1.name{3},'A3');
%$     t(7) = dassert(ts1.data,[A(:,1), exp(A(:,2)), A(:,3)],1e-15);
%$ end
288 289 290 291 292 293 294 295 296 297
%$ T = all(t);
%@eof:2

%@test:3
%$ % Define a datasets.
%$ A = rand(10,3);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$
298 299
%$ try
%$     % Apply the logarithm function to the first and third variables.
300 301
%$     ts1{'A1'} = ts1{'A1'}.log;
%$     ts1{'A3'} = ts1{'A3'}.log;
302 303 304 305
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
306
%$
307 308 309 310 311 312 313 314
%$ if t(1)
%$     t(2) = dassert(ts1.vobs,3);
%$     t(3) = dassert(ts1.nobs,10);
%$     t(4) = dassert(ts1.name{2},'A2');
%$     t(5) = dassert(ts1.name{1},'A1');
%$     t(6) = dassert(ts1.name{3},'A3');
%$     t(7) = dassert(ts1.data,[log(A(:,1)), A(:,2), log(A(:,3))],1e-15);
%$ end
315 316 317 318 319 320 321 322 323 324
%$ T = all(t);
%@eof:3

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

%@test:5
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,3);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'A1';'B2';'B3'},[]);
%$
350 351 352 353 354 355
%$ try
%$     ts1.A1 = ts2.A1;
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
356
%$
357 358 359 360 361 362 363
%$ if t(1)
%$     t(2) = dassert(ts1.vobs,3);
%$     t(3) = dassert(ts1.nobs,10);
%$     t(4) = dassert(ts1.name{1},'A1');
%$     t(5) = dassert(ts1.data(:,1),B(:,1), 1e-15);
%$     t(6) = dassert(ts1.data(:,2:3),A(:,2:3), 1e-15);
%$ end
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
%$ T = all(t);
%@eof:5

%@test:6
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,2);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'B1';'B2'},[]);
%$
%$ % Call tested routine.
%$ try
%$     ts1.B2 = ts2.B2.log;
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ if t(1)
%$    t(2) = dassert(ts1.vobs,4);
%$    t(3) = dassert(ts1.nobs,10);
%$    t(4) = dassert(ts1.name{1},'A1');
%$    t(5) = dassert(ts1.name{2},'A2');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(ts1.name{4},'B2');
%$    t(8) = dassert(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);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'B1';'B2'},[]);
%$
403 404
%$ try
%$     % Append B2 to the first object.
405
%$     ts1{'B2'} = ts2{'B2'};
406 407 408 409 410
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
%$ if t(1)
%$    t(2) = dassert(ts1.vobs,4);
%$    t(3) = dassert(ts1.nobs,10);
%$    t(4) = dassert(ts1.name{1},'A1');
%$    t(5) = dassert(ts1.name{2},'A2');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(6) = dassert(ts1.name{4},'B2');
%$    t(7) = dassert(ts1.data,[A(:,1), A(:,2), A(:,3), B(:,2)],1e-15);
%$ end
%$ T = all(t);
%@eof:7

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

%@test:9
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,2);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'A1';'B1'},[]);
%$
%$ % modify first object.
%$ try
461
%$     ts1{'A1','A4'} = ts2;
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ if t(1)
%$    t(2) = dassert(ts1.vobs,4);
%$    t(3) = dassert(ts1.nobs,10);
%$    t(4) = dassert(ts1.name{2},'A2');
%$    t(5) = dassert(ts1.name{1},'A1');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(ts1.name{4},'A4');
%$    t(8) = dassert(ts1.data,[B(:,1), A(:,2:3), B(:,2)],1e-15);
%$ end
%$ T = all(t);
%@eof:9


%@test:10
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,3);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,[],{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,[],{'A1';'B1';'B2'},[]);
%$
%$ % 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) = dassert(ts1.vobs,4);
%$    t(3) = dassert(ts1.nobs,10);
%$    t(4) = dassert(ts1.name{1},'A1');
%$    t(5) = dassert(ts1.name{2},'A2');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(ts1.name{4},'A4');
%$    t(8) = dassert(ts1.data,[B(:,1:2), A(:,3), B(:,3)],1e-15);
%$ end
%$ T = all(t);
%@eof:10

%@test:11
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(10,5);
%$
%$ % 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'},[]);
%$
%$ % 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) = dassert(ts1.vobs,4);
%$    %t(3) = dassert(ts1.nobs,10);
%$    %t(4) = dassert(ts1.name,{'A1','A2';'A3';'B1';'B2'});
%$    %t(5) = dassert(ts1.data,[B(:,1:2), A(:,3), B(:,3:4)],1e-15);
%$ end
%$ T = all(t);
%@eof:11

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

%@test:13
%$ % Define a datasets.
%$ A = rand(40,3); B = rand(40,1);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
%$
%$ % modify first object.
%$ try
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
%$     rg = d1:d2;
578
%$     ts1.A1(rg) = B(3:7);
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ if t(1)
%$    t(2) = dassert(ts1.vobs,3);
%$    t(3) = dassert(ts1.nobs,40);
%$    t(4) = dassert(ts1.name{2},'A2');
%$    t(5) = dassert(ts1.name{1},'A1');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(ts1.data,[[A(1:2,1); B(3:7); A(8:end,1)], A(:,2:3)],1e-15);
%$ end
%$ T = all(t);
%@eof:13

%@test:14
%$ % Define a datasets.
%$ A = rand(40,3); B = rand(40,1);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
%$
%$ % modify first object.
%$ try
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
%$     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) = dassert(ts1.vobs,3);
%$    t(3) = dassert(ts1.nobs,40);
%$    t(4) = dassert(ts1.name{2},'A2');
%$    t(5) = dassert(ts1.name{1},'A1');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(ts1.data,[[A(1:2,1); B(3:7); A(8:end,1)], A(:,2:3)],1e-15);
%$ end
%$ T = all(t);
%@eof:14

%@test:15
%$ % Define a datasets.
%$ A = rand(40,3); B = rand(40,1);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
%$
%$ % modify first object.
%$ try
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
%$     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) = dassert(ts1.vobs,3);
%$    t(3) = dassert(ts1.nobs,40);
%$    t(4) = dassert(ts1.name{2},'A2');
%$    t(5) = dassert(ts1.name{1},'A1');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(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);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
%$
%$ % modify first object.
%$ try
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
%$     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) = dassert(ts1.vobs,3);
%$    t(3) = dassert(ts1.nobs,40);
%$    t(4) = dassert(ts1.name{2},'A2');
%$    t(5) = dassert(ts1.name{1},'A1');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(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);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
%$
%$ % modify first object.
%$ try
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
%$     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) = dassert(ts1.vobs,3);
%$    t(3) = dassert(ts1.nobs,40);
%$    t(4) = dassert(ts1.name{2},'A2');
%$    t(5) = dassert(ts1.name{1},'A1');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(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);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$ ts2 = dseries(B,'1950Q1',{'B1'},[]);
%$
%$ % modify first object.
%$ try
%$     d1 = dates('1950Q3');
%$     d2 = dates('1951Q3');
%$     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) = dassert(ts1.vobs,3);
%$    t(3) = dassert(ts1.nobs,40);
%$    t(4) = dassert(ts1.name{2},'A2');
%$    t(5) = dassert(ts1.name{1},'A1');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(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);
%@eof:18

%@test:19
%$ % Define a datasets.
%$ A = rand(40,3);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$
%$ % Instantiate a dates object.
%$ dd = dates('1952Q1');
%$
%$ % 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) = dassert(ts1.vobs,3);
%$    t(3) = dassert(ts1.nobs,40);
%$    t(4) = dassert(ts1.name{2},'A2');
%$    t(5) = dassert(ts1.name{1},'A1');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(ts1.data,A,1e-15);
%$    t(8) = dassert(ts1.init,dd);
%$    t(9) = dassert(ts1.dates(1),dd);
%$ end
%$ T = all(t);
%@eof:19

%@test:20
%$ % Define a datasets.
%$ A = rand(40,3);
%$
%$ % Instantiate two dseries object.
%$ ts1 = dseries(A,'1950Q1',{'A1';'A2';'A3'},[]);
%$
%$ % Instantiate a dates object.
%$ dd = dates('1952Q1');
%$
%$ % modify first object.
%$ try
%$     ts1.dates = dd:(dd+(ts1.nobs-1));
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dassert(ts1.vobs,3);
%$    t(3) = dassert(ts1.nobs,40);
%$    t(4) = dassert(ts1.name{2},'A2');
%$    t(5) = dassert(ts1.name{1},'A1');
%$    t(6) = dassert(ts1.name{3},'A3');
%$    t(7) = dassert(ts1.data,A,1e-15);
%$    t(8) = dassert(ts1.init,dd);
%$    t(9) = dassert(ts1.dates(1),dd);
%$ end
%$ T = all(t);
%@eof:20

%@test:21
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
%$ % Define a datasets.
%$ A = rand(4,3);
%$
%$ % Instantiate an empty dseries object.
%$ ts = dseries(dates('1950Q1'));
%$
%$ % Populate ts
%$ try
%$     ts(:) = A;
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ % Instantiate a time series object.
%$ if t(1)
%$    t(2) = dassert(ts.vobs,3);
%$    t(3) = dassert(ts.nobs,4);
%$    t(4) = dassert(ts.data,A,1e-15);
%$ end
%$ T = all(t);
%@eof:21

%@test:22
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
%$ % 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);
860
%@eof:22