Commit c023fdd7 authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Rewrote @dynSeries/subsref recursively. Improved error messages. Fixed bugs (missing time member).

parent 52b0f6db
function us = subsref(ts, S) function B = subsref(A, S)
%@info: %@info:
%! @deftypefn {Function File} {@var{us} =} subsref (@var{ts},S) %! @deftypefn {Function File} {@var{us} =} subsref (@var{ts},S)
%! @anchor{@dynSeries/subsref} %! @anchor{@dynSeries/subsref}
...@@ -44,7 +44,7 @@ function us = subsref(ts, S) ...@@ -44,7 +44,7 @@ function us = subsref(ts, S)
%! @end deftypefn %! @end deftypefn
%@eod: %@eod:
% Copyright (C) 2011, 2012 Dynare Team % Copyright (C) 2011, 2012, 2013 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
...@@ -61,111 +61,88 @@ function us = subsref(ts, S) ...@@ -61,111 +61,88 @@ function us = subsref(ts, S)
% 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/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr switch S(1).type
case '.'
if length(S)==1 && isequal(S.type,'.') switch S(1).subs
switch S.subs
case {'data','nobs','vobs','name','tex','freq','time','init'} % Public members. case {'data','nobs','vobs','name','tex','freq','time','init'} % Public members.
us = builtin('subsref', ts, S); B = builtin('subsref', A, S(1));
case {'log','exp','ygrowth','qgrowth','ydiff','qdiff'} % Give "dot access" to public methods. case {'log','exp','ygrowth','qgrowth','ydiff','qdiff'} % Give "dot access" to public methods.
us = feval(S.subs,ts); B = feval(S(1).subs,A);
case {'save'} case {'save'} % Save dynSeries object on disk (default is a csv file).
us = NaN; us = NaN;
save(ts); if length(S)==2 && strcmp(S(2).type,'()')
save(A,S(2).subs{:});
S = shiftS(S);
else
save(A);
end
otherwise % Extract a sub-object by selecting one variable. otherwise % Extract a sub-object by selecting one variable.
ndx = strmatch(S.subs,ts.name); ndx = strmatch(S(1).subs,A.name,'exact');
if ~isempty(ndx) if ~isempty(ndx)
us = dynSeries(); B = dynSeries();
us.data = ts.data(:,ndx); B.data = A.data(:,ndx);
us.name = deblank(ts.name(ndx,:)); B.name = deblank(A.name(ndx,:));
us.tex = deblank(ts.tex(ndx,:)); B.tex = deblank(A.tex(ndx,:));
us.nobs = ts.nobs; B.nobs = A.nobs;
us.vobs = 1; B.vobs = 1;
us.freq = ts.freq; B.freq = A.freq;
us.init = ts.init; B.init = A.init;
return B.time = A.time;
else else
error('dynSeries::subsref: Unknown public method, public member or variable!') error('dynSeries::subsref: Unknown public method, public member or variable!')
end end
end end
return case '()'
end if ischar(S(1).subs{1})
% If ts is an empty dynSeries object, populate this object by reading data in a file.
if length(S)==1 && isequal(S.type,'()') if isempty(A)
if ischar(S.subs{1}) B = dynSeries(S(1).subs{1});
us = dynSeries(S.subs{1}); else
elseif isa(S.subs{1},'dynDates') error(['dynSeries::subsref: dynSeries object ''' inputname(1) ''' is not empty!'])
[junk,tdx] = intersect(ts.time.time,S.subs{1}.time,'rows'); end
us = dynSeries(); elseif isa(S(1).subs{1},'dynDates')
us.data = ts.data(tdx,:); % Extract a subsample using a dynDates object
us.time = ts.time(tdx,:); [junk,tdx] = intersect(A.time.time,S(1).subs{1}.time,'rows');
us.name = deblank(ts.name); B = dynSeries();
us.tex = deblank(ts.tex); B.data = A.data(tdx,:);
us.nobs = length(tdx); B.name = deblank(A.name);
us.vobs = ts.vobs; B.tex = deblank(A.tex);
us.freq = ts.freq; B.nobs = length(tdx);
us.init = ts.init+tdx(1); B.vobs = A.vobs;
else B.freq = A.freq;
% Extract a sub-object by selecting a sub-sample. B.init = A.init+tdx(1);
if size(ts.data,2)>1 B.time = A.time(tdx,:);
S.subs = [S.subs, ':']; elseif isvector(S(1).subs{1}) && all(isint(S(1).subs{1}))
% Extract a subsample using a vector of integers (observation index).
if all(S(1).subs{1}>0) && all(S(1).subs{1}<=A.nobs)
if size(A.data,2)>1
S(1).subs = [S(1).subs, ':'];
end
B.data = builtin('subsref', A.data, S(1));
B.nobs = size(B.data,1);
B.vobs = A.vobs;
B.freq = A.freq;
B.time = builtin('subsref', A.time, S(1));
B.init = A.init+S(1).subs{1}(1);
B.name = A.name;
B.tex = A.tex;
else
error('dynSeries::subsref: Indices are out of bounds!')
end end
us.data = builtin('subsref', ts.data, S);
us.nobs = size(us.data,1);
us.vobs = ts.vobs;
us.freq = ts.freq;
us.time = builtin('subsref', ts.time, S);
us.init = ts.init+S.subs{1}(1);
us.name = ts.name;
us.tex = ts.tex;
return
end
end
if (length(S)==2) && (isequal(S(1).subs,'init'))
if isequal(S(2).type,'.') && ( isequal(S(2).subs,'freq') || isequal(S(2).subs,'time') )
us = builtin('subsref', ts.init, S(2));
else else
error('dynSeries:subsref:: I don''t understand what you are trying to do!') error('dynSeries::subsref: I have no idea of what you are trying to do!')
end end
return case '{}'
B = extract(A,S(1).subs{:});
otherwise
error('dynSeries::subsref: What the Hell are you doin'' here?!')
end end
if (length(S)==2) && (isequal(S(1).type,'.')) && (isequal(S(1).subs,'data')) && (isequal(S(2).type,'()')) S = shiftS(S);
us = builtin('subsref',ts.data,S(2)); if ~isempty(S)
return B = subsref(B, S);
end end
if (length(S)==1) && isequal(S(1).type,'{}')
us = extract(ts,S(1).subs{:});
return
end
if (length(S)==2) && isequal(S(1).type,'{}')
us = extract(ts,S(1).subs{:});
us = subsref(us, S(2));
return
end
if (length(S)==2) && isequal(S(1).subs,'save') && isequal(S(1).type,'.') && isequal(S(2).type,'()')
us = NaN;
save(ts,S(2).subs{:});
return
end
if (length(S)==2) && isequal(S(1).subs,'set_names') && isequal(S(1).type,'.') && isequal(S(2).type,'()')
us = set_names(ts,S(2).subs{:});
return
end
if (length(S)==2) && isequal(S(1).subs,'name') && isequal(S(1).type,'.') && isequal(S(2).type,'{}')
us = ts.name{S(2).subs{1}};
return
end
%@test:1 %@test:1
%$ % Define a data set. %$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)]; %$ A = [transpose(1:10),2*transpose(1:10)];
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment