Home > matlab > @dynSeries > subsref.m

subsref

PURPOSE ^

@info:

SYNOPSIS ^

function us = subsref(ts, S)

DESCRIPTION ^

@info:
! @deftypefn {Function File} {@var{us} =} subsref (@var{ts},S)
! @anchor{@dynSeries/subsref}
! @sp 1
! Overloads the subsref method for the Dynare time series class (@ref{dynSeries}).
! @sp 2
! @strong{Inputs}
! @sp 1
! @table @ @var
! @item ts
! Dynare time series object instantiated by @ref{dynSeries}.
! @item S
! Matlab's structure array S with two fields, type and subs. The type field is string containing '()', '@{@}', or '.', where '()' specifies
! integer subscripts, '@{@}' specifies cell array subscripts, and '.' specifies subscripted structure fields. The subs field is a cell array
! or a string containing the actual subscripts (see matlab's documentation).
! @end table
! @sp 1
! @strong{Outputs}
! @sp 1
! @table @ @var
! @item us
! Dynare time series object. Depending on the calling sequence @var{us} is a transformation of @var{ts} obtained by applying a public method on @var{ts},
! or a dynSeries object built by extracting a variable from @var{ts}, or a dynSeries object containing a subsample of the all the variable in @var{ts}.
! @end table
! @sp 2
! @strong{Example 1.} Let @var{ts} be a dynSeries object containing three variables named 'A1', 'A2' and 'A3'. Then the following syntax:
! @example
!   us = ts.A1;
! @end example
!will create a new dynSeries object @var{us} containing the variable 'A1'.
! @sp 1
! @strong{Example 2.} Let @var{ts} be a dynSeries object. Then the following syntax:
! @example
!   us = ts.log;
! @end example
!will create a new dynSeries object @var{us} containing all the variables of @var{ts} transformed by the neperian logarithm.
! @sp 1
! @strong{Example 3.} Let @var{ts} be a dynSeries object. The following syntax:
! @example
!   us = ts(3:50);
! @end example
!will create a new dynSeries object @var{us} by selecting a subsample out of @var{ts}.
! @sp 2
! @strong{This function is called by:}
! @sp 2
! @strong{This function calls:}
! @ref{dynSeries}, @ref{log}, @ref{exp}
!
! @end deftypefn
@eod:

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function us = subsref(ts, S)
0002 %@info:
0003 %! @deftypefn {Function File} {@var{us} =} subsref (@var{ts},S)
0004 %! @anchor{@dynSeries/subsref}
0005 %! @sp 1
0006 %! Overloads the subsref method for the Dynare time series class (@ref{dynSeries}).
0007 %! @sp 2
0008 %! @strong{Inputs}
0009 %! @sp 1
0010 %! @table @ @var
0011 %! @item ts
0012 %! Dynare time series object instantiated by @ref{dynSeries}.
0013 %! @item S
0014 %! Matlab's structure array S with two fields, type and subs. The type field is string containing '()', '@{@}', or '.', where '()' specifies
0015 %! integer subscripts, '@{@}' specifies cell array subscripts, and '.' specifies subscripted structure fields. The subs field is a cell array
0016 %! or a string containing the actual subscripts (see matlab's documentation).
0017 %! @end table
0018 %! @sp 1
0019 %! @strong{Outputs}
0020 %! @sp 1
0021 %! @table @ @var
0022 %! @item us
0023 %! Dynare time series object. Depending on the calling sequence @var{us} is a transformation of @var{ts} obtained by applying a public method on @var{ts},
0024 %! or a dynSeries object built by extracting a variable from @var{ts}, or a dynSeries object containing a subsample of the all the variable in @var{ts}.
0025 %! @end table
0026 %! @sp 2
0027 %! @strong{Example 1.} Let @var{ts} be a dynSeries object containing three variables named 'A1', 'A2' and 'A3'. Then the following syntax:
0028 %! @example
0029 %!   us = ts.A1;
0030 %! @end example
0031 %!will create a new dynSeries object @var{us} containing the variable 'A1'.
0032 %! @sp 1
0033 %! @strong{Example 2.} Let @var{ts} be a dynSeries object. Then the following syntax:
0034 %! @example
0035 %!   us = ts.log;
0036 %! @end example
0037 %!will create a new dynSeries object @var{us} containing all the variables of @var{ts} transformed by the neperian logarithm.
0038 %! @sp 1
0039 %! @strong{Example 3.} Let @var{ts} be a dynSeries object. The following syntax:
0040 %! @example
0041 %!   us = ts(3:50);
0042 %! @end example
0043 %!will create a new dynSeries object @var{us} by selecting a subsample out of @var{ts}.
0044 %! @sp 2
0045 %! @strong{This function is called by:}
0046 %! @sp 2
0047 %! @strong{This function calls:}
0048 %! @ref{dynSeries}, @ref{log}, @ref{exp}
0049 %!
0050 %! @end deftypefn
0051 %@eod:
0052 
0053 % Copyright (C) 2011 Dynare Team
0054 %
0055 % This file is part of Dynare.
0056 %
0057 % Dynare is free software: you can redistribute it and/or modify
0058 % it under the terms of the GNU General Public License as published by
0059 % the Free Software Foundation, either version 3 of the License, or
0060 % (at your option) any later version.
0061 %
0062 % Dynare is distributed in the hope that it will be useful,
0063 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0064 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0065 % GNU General Public License for more details.
0066 %
0067 % You should have received a copy of the GNU General Public License
0068 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0069 
0070 % AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
0071 
0072 if isequal(S.type,'.') & length(S)==1
0073     switch S.subs
0074       case {'data','nobs','vobs','name','tex','freq','time','init','last','Time'} % Public members.
0075         us = builtin('subsref', ts, S);
0076       case {'log','exp'}                                                   % Give "dot access" to public methods.
0077         us = feval(S.subs,ts);
0078       otherwise                                                            % Extract a sub-object by selecting one variable.
0079         ndx = strmatch(S.subs,ts.name);
0080         if ~isempty(ndx)
0081             us = dynSeries();
0082             us.data = ts.data(:,ndx);
0083             us.name = deblank(ts.name(ndx,:));
0084             us.tex  = deblank(ts.tex(ndx,:));
0085             us.nobs = ts.nobs;
0086             us.vobs = 1;
0087             us.freq = ts.freq;
0088             us.time = ts.time;
0089             us.init = ts.init;
0090             us.last = ts.last;
0091             return
0092         else
0093             error('dynSeries::subsref: Unknown public method, public member or variable!')
0094         end
0095     end
0096     return
0097 end
0098 if isequal(S.type,'()') & length(S)==1                                                    % Extract a sub-object by selecting a sub-sample.
0099     us = dynSeries();
0100     if size(ts.data,2)>1
0101         S.subs = [S.subs, ':'];
0102     end
0103     us.data = builtin('subsref', ts.data, S);
0104     us.nobs = size(us.data,1);
0105     us.vobs = ts.vobs;
0106     us.freq = ts.freq;
0107     us.time = builtin('subsref', ts.time, S);
0108     us.init = us.time(1,:);
0109     us.last = us.time(end,:);
0110     us.name = ts.name;
0111     us.tex  = ts.tex;
0112 end
0113 
0114 if (length(S)==2) & (isequal(S(1).subs,'Time'))
0115     if isequal(S(2).type,'.')
0116         us = builtin('subsref', ts.Time, S(2));
0117     else
0118         error('dynSeries:subsref:: I don''t understand what you are trying to do!')
0119     end
0120 end
0121 
0122 %@test:1
0123 %$ addpath ../matlab
0124 %$ % Define a data set.
0125 %$ A = [transpose(1:10),2*transpose(1:10)];
0126 %$
0127 %$ % Define names
0128 %$ A_name = char('A1','A2');
0129 %$
0130 %$ % Instantiate a time series object.
0131 %$ ts1 = dynSeries(A,[],A_name,[]);
0132 %$
0133 %$ % Call the tested method.
0134 %$ a = ts1(2:9);
0135 %$
0136 %$ % Expected results.
0137 %$ e.data = [transpose(2:9),2*transpose(2:9)];
0138 %$ e.nobs = 8;
0139 %$ e.vobs = 2;
0140 %$ e.name = char('A1','A2');
0141 %$ e.freq = 1;
0142 %$ tmp = ts1.time; e.time = tmp(2:9,:);
0143 %$ e.init = e.time(1,:);
0144 %$ e.last = e.time(end,:);
0145 %$
0146 %$ % Check the results.
0147 %$ t(1) = dyn_assert(a.data,e.data);
0148 %$ t(2) = dyn_assert(a.time,e.time);
0149 %$ t(3) = dyn_assert(a.nobs,e.nobs);
0150 %$ t(4) = dyn_assert(a.vobs,e.vobs);
0151 %$ t(5) = dyn_assert(a.freq,e.freq);
0152 %$ t(6) = dyn_assert(a.init,e.init);
0153 %$ t(7) = dyn_assert(a.last,e.last);
0154 %$ T = all(t);
0155 %@eof:1
0156 
0157 %@test:2
0158 %$ addpath ../matlab
0159 %$ % Define a data set.
0160 %$ A = [transpose(1:10),2*transpose(1:10)];
0161 %$
0162 %$ % Define names
0163 %$ A_name = char('A1','A2');
0164 %$
0165 %$ % Instantiate a time series object.
0166 %$ ts1 = dynSeries(A,[],A_name,[]);
0167 %$
0168 %$ % Call the tested method.
0169 %$ a = ts1.A1;
0170 %$
0171 %$ % Expected results.
0172 %$ e.data = transpose(1:10);
0173 %$ e.nobs = 10;
0174 %$ e.vobs = 1;
0175 %$ e.name = char('A1');
0176 %$ e.freq = 1;
0177 %$ e.time = [transpose(1:10),ones(10,1)];
0178 %$ e.init = e.time(1,:);
0179 %$ e.last = e.time(end,:);
0180 %$
0181 %$ % Check the results.
0182 %$ t(1) = dyn_assert(a.data,e.data);
0183 %$ t(2) = dyn_assert(a.time,e.time);
0184 %$ t(3) = dyn_assert(a.nobs,e.nobs);
0185 %$ t(4) = dyn_assert(a.vobs,e.vobs);
0186 %$ t(5) = dyn_assert(a.freq,e.freq);
0187 %$ t(6) = dyn_assert(a.init,e.init);
0188 %$ t(7) = dyn_assert(a.last,e.last);
0189 %$ T = all(t);
0190 %@eof:2
0191 
0192 %@test:3
0193 %$ addpath ../matlab
0194 %$ % Define a data set.
0195 %$ A = [transpose(1:10),2*transpose(1:10)];
0196 %$
0197 %$ % Define names
0198 %$ A_name = char('A1','A2');
0199 %$
0200 %$ % Instantiate a time series object.
0201 %$ ts1 = dynSeries(A,[],A_name,[]);
0202 %$
0203 %$ % Call the tested method.
0204 %$ a = ts1.log;
0205 %$
0206 %$ % Expected results.
0207 %$ e.data = log(A);
0208 %$ e.nobs = 10;
0209 %$ e.vobs = 2;
0210 %$ e.name = char('A1','A2');
0211 %$ e.freq = 1;
0212 %$ tmp = ts1.time; e.time = tmp(1:10,:);
0213 %$ e.init = e.time(1,:);
0214 %$ e.last = e.time(end,:);
0215 %$
0216 %$ % Check the results.
0217 %$ t(1) = dyn_assert(a.data,e.data);
0218 %$ t(2) = dyn_assert(a.time,e.time);
0219 %$ t(3) = dyn_assert(a.nobs,e.nobs);
0220 %$ t(4) = dyn_assert(a.vobs,e.vobs);
0221 %$ t(5) = dyn_assert(a.freq,e.freq);
0222 %$ t(6) = dyn_assert(a.init,e.init);
0223 %$ t(7) = dyn_assert(a.last,e.last);
0224 %$ T = all(t);
0225 %@eof:3

Generated on Mon 21-May-2012 02:42:43 by m2html © 2005