Rewrote @dynSeries/subsasgn.
The following syntaxes are now allowed: ts{'VARIABLE_NAME'}(range) = data; where range is a dynDates object and data is an array of doubles, or ts{'VARIABLE_NAME'}(range) = TS{'OTHER_VARIABLE_NAME'}(range) where ts and TS are dynSeries objects.time-shift
parent
b5c3ef2ade
commit
5ededa8848
|
@ -25,102 +25,167 @@ function A = subsasgn(A,S,B)
|
||||||
% You should have received a copy of the GNU General Public License
|
% You should have received a copy of the GNU General Public License
|
||||||
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
switch S(1).type
|
merge_dynSeries_objects = 1;
|
||||||
case '{}' % Multiple variable selection.
|
|
||||||
if ~isequal(numel(S(1).subs),numel(unique(S(1).subs)))
|
switch length(S)
|
||||||
error('dynSeries::subsasgn: Wrong syntax!')
|
case 1
|
||||||
end
|
switch S(1).type
|
||||||
for i=1:numel(S(1).subs)
|
case '{}' % Multiple variable selection.
|
||||||
element = S(1).subs{i};
|
if ~isequal(numel(S(1).subs),numel(unique(S(1).subs)))
|
||||||
idArobase = strfind(element,'@');
|
error('dynSeries::subsasgn: Wrong syntax!')
|
||||||
if ~isempty(idArobase)
|
end
|
||||||
switch length(idArobase)
|
for i=1:numel(S(1).subs)
|
||||||
case 2
|
element = S(1).subs{i};
|
||||||
idComma = strfind(element(idArobase(1)+1:idArobase(2)-1),',');
|
idArobase = strfind(element,'@');
|
||||||
if ~isempty(idComma)
|
if ~isempty(idArobase)
|
||||||
elements = cell(1,numel(idComma)+1); j = 1;
|
switch length(idArobase)
|
||||||
expression = element(idArobase(1)+1:idArobase(2)-1);
|
case 2
|
||||||
while ~isempty(expression)
|
idComma = strfind(element(idArobase(1)+1:idArobase(2)-1),',');
|
||||||
[token, expression] = strtok(expression,',');
|
if ~isempty(idComma)
|
||||||
elements(j) = {[element(1:idArobase(1)-1), token, element(idArobase(2)+1:end)]};
|
elements = cell(1,numel(idComma)+1); j = 1;
|
||||||
j = 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
|
||||||
|
error('dynSeries::subsasgn: Wrong syntax, matlab''s regular expressions cannot be used here!')
|
||||||
|
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
|
||||||
|
error('dynSeries::subsasgn: Wrong syntax, matlab''s regular expressions cannot be used here!')
|
||||||
|
end
|
||||||
|
otherwise
|
||||||
|
error('dynSeries::subsasgn: Wrong syntax!')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if ~isequal(length(S(1).subs),B.vobs)
|
||||||
|
error('dynSeries::subsasgn: Wrong syntax!')
|
||||||
|
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.
|
||||||
|
id = strmatch(S(1).subs{i},A.name);
|
||||||
|
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
|
||||||
|
case '.' % Single variable selection.
|
||||||
|
if ~isequal(S(1).subs,B.name)
|
||||||
|
if ~isequal(S(1).subs,B.name{1})
|
||||||
|
% Rename a variable.
|
||||||
|
id = strmatch(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 isa(S(1).subs{1},'dynDates') || isa(S(1).subs{1},'dynDate')
|
||||||
|
[junk, tdx] = intersect(A.time.time,S(1).subs{1}.time,'rows');
|
||||||
|
if isa(B,'dynSeries')
|
||||||
|
[junk, tdy] = intersect(B.time.time,S(1).subs{1}.time,'rows');
|
||||||
|
if isempty(tdy)
|
||||||
|
error('dynSeries::subsasgn: Periods of the dynSeries objects on the left and right hand sides must intersect!')
|
||||||
|
end
|
||||||
|
if ~isequal(A.vobs,B.vobs)
|
||||||
|
error('dynSeries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
|
||||||
|
end
|
||||||
|
A.data(tdx,:) = B.data(tdy,:);
|
||||||
|
elseif isnumeric(B)
|
||||||
|
merge_dynSeries_objects = 0;
|
||||||
|
if isequal(length(tdx),rows(B))
|
||||||
|
if isequal(columns(A.data),columns(B))
|
||||||
|
A.data(tdx,:) = B;
|
||||||
|
else
|
||||||
|
error('dynSeries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
|
||||||
|
end
|
||||||
|
else
|
||||||
|
error('dynSeries::subsassgn: Dimension error! The number of periods on the left and right hand side must match.')
|
||||||
|
end
|
||||||
|
else
|
||||||
|
error('dynSeries::subsasgn: The object on the right hand side must be a dynSeries object or a numeric array!')
|
||||||
|
end
|
||||||
|
else
|
||||||
|
error('dynSeries::subsasgn: Wrong syntax!')
|
||||||
|
end
|
||||||
|
otherwise
|
||||||
|
error('dynSeries::subsasgn: Wrong syntax!')
|
||||||
|
end
|
||||||
|
case 2
|
||||||
|
merge_dynSeries_objects = 0;
|
||||||
|
if ((isequal(S(1).type,'{}') || isequal(S(1).type,'.')) && isequal(S(2).type,'()'))
|
||||||
|
sA = extract(A,S(1).subs{:});
|
||||||
|
if (isa(B,'dynSeries') && isequal(sA.vobs,B.vobs)) || (isnumeric(B) && isequal(sA.vobs,columns(B)))
|
||||||
|
if isa(S(2).subs{1},'dynDates') || isa(S(2).subs{1},'dynDate')
|
||||||
|
[junk, tdx] = intersect(sA.time.time,S(2).subs{1}.time,'rows');
|
||||||
|
if isa(B,'dynSeries')
|
||||||
|
[junk, tdy] = intersect(B.time.time,S(2).subs{1}.time,'rows');
|
||||||
|
if isempty(tdy)
|
||||||
|
error('dynSeries::subsasgn: Periods of the dynSeries objects on the left and right hand sides must intersect!')
|
||||||
end
|
end
|
||||||
S(1).subs = replace_object_in_a_one_dimensional_cell_array(S(1).subs, elements(:), i);
|
sA.data(tdx,:) = B.data(tdy,:);
|
||||||
else
|
elseif isnumeric(B)
|
||||||
error('dynSeries::subsasgn: Wrong syntax, matlab''s regular expressions cannot be used here!')
|
merge_dynSeries_objects = 0;
|
||||||
end
|
if isequal(length(tdx),rows(B))
|
||||||
case 4
|
if isequal(columns(sA.data),columns(B))
|
||||||
idComma_1 = strfind(element(idArobase(1)+1:idArobase(2)-1),',');
|
sA.data(tdx,:) = B;
|
||||||
idComma_2 = strfind(element(idArobase(3)+1:idArobase(4)-1),',');
|
else
|
||||||
if ~isempty(idComma_1)
|
error('dynSeries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
|
||||||
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
|
||||||
|
else
|
||||||
|
error('dynSeries::subsassgn: Dimension error! The number of periods on the left and right hand side must match.')
|
||||||
end
|
end
|
||||||
S(1).subs = replace_object_in_a_one_dimensional_cell_array(S(1).subs, elements(:), i);
|
|
||||||
else
|
else
|
||||||
error('dynSeries::subsasgn: Wrong syntax, matlab''s regular expressions cannot be used here!')
|
error('dynSeries::subsasgn: The object on the right hand side must be a dynSeries object or a numeric array!')
|
||||||
end
|
end
|
||||||
otherwise
|
else
|
||||||
error('dynSeries::subsasgn: Wrong syntax!')
|
error('dynSeries::subsasgn: Wrong syntax!')
|
||||||
end
|
end
|
||||||
|
A = merge(A,sA);
|
||||||
|
else
|
||||||
|
error('dynSeries::subsasgn: Dimension error! The number of variables on the left and right hand side must match.')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if ~isequal(length(S(1).subs),B.vobs)
|
|
||||||
error('dynSeries::subsasgn: Wrong syntax!')
|
|
||||||
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.
|
|
||||||
id = strmatch(S(1).subs{i},A.name);
|
|
||||||
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
|
|
||||||
case '.' % Single variable selection.
|
|
||||||
if ~isequal(S(1).subs,B.name)
|
|
||||||
if ~isequal(S(1).subs,B.name{1})
|
|
||||||
% Rename a variable.
|
|
||||||
id = strmatch(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
|
|
||||||
error('Not yet implemented!')
|
|
||||||
otherwise
|
otherwise
|
||||||
error('dynSeries::subsasgn: Wrong syntax!')
|
error('dynSeries::subsasgn: Wrong syntax!')
|
||||||
end
|
end
|
||||||
|
|
||||||
A = merge(A,B);
|
|
||||||
|
|
||||||
S = shiftS(S);
|
if merge_dynSeries_objects
|
||||||
if ~isempty(S)
|
A = merge(A,B);
|
||||||
error('dynSeries::subsasgn: Wrong syntax!')
|
|
||||||
A = subsasgn(A, S, B);
|
|
||||||
end
|
end
|
||||||
|
|
||||||
%@test:1
|
%@test:1
|
||||||
|
|
Loading…
Reference in New Issue