Home > matlab > @dynDate > dynDate.m

dynDate

PURPOSE ^

@info:

SYNOPSIS ^

function date = dynDate(a)

DESCRIPTION ^

@info:
! @deftypefn {Function File} {@var{date} =} dynDate (@var{a})
! @anchor{dynDate}
! @sp 1
! Constructor for the Dynare dates class.
! @sp 2
! @strong{Inputs}
! @sp 1
! @table @ @var
! @item a
! Date. For Quaterly, Monthly or Weekly data, a must be a string. For yearly data or if the frequence is not
! defined  must be an integer. If @var{a} is a dynDate object, then date will be a copy of this object. If
! the constructor is called without input argument, it will return an empty dynDate object.
! @end table
! @sp 1
! @strong{Outputs}
! @sp 1
! @table @ @var
! @item date
! Dynare date object.
! @end table
! @sp 1
! @strong{Properties}
! @sp 1
! The constructor defines the following properties:
! @sp 1
! @table @ @var
! @item freq
! Scalar integer, the frequency of the time series. @var{freq} is equal to 1 if data are on a yearly basis or if
! frequency is unspecified. @var{freq} is equal to 4 if data are on a quaterly basis. @var{freq} is equal to
! 12 if data are on a monthly basis. @var{freq} is equal to 52 if data are on a weekly basis.
! @item time
! Row vector of integers (1*2) indicating the year and the week, month or quarter of the first observation.
! @end table
! @sp 1
! @strong{This function is called by:}
! @sp 2
! @strong{This function calls:}
! @ref{set_time}
!
! @end deftypefn
@eod:

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function date = dynDate(a)
0002 
0003 %@info:
0004 %! @deftypefn {Function File} {@var{date} =} dynDate (@var{a})
0005 %! @anchor{dynDate}
0006 %! @sp 1
0007 %! Constructor for the Dynare dates class.
0008 %! @sp 2
0009 %! @strong{Inputs}
0010 %! @sp 1
0011 %! @table @ @var
0012 %! @item a
0013 %! Date. For Quaterly, Monthly or Weekly data, a must be a string. For yearly data or if the frequence is not
0014 %! defined  must be an integer. If @var{a} is a dynDate object, then date will be a copy of this object. If
0015 %! the constructor is called without input argument, it will return an empty dynDate object.
0016 %! @end table
0017 %! @sp 1
0018 %! @strong{Outputs}
0019 %! @sp 1
0020 %! @table @ @var
0021 %! @item date
0022 %! Dynare date object.
0023 %! @end table
0024 %! @sp 1
0025 %! @strong{Properties}
0026 %! @sp 1
0027 %! The constructor defines the following properties:
0028 %! @sp 1
0029 %! @table @ @var
0030 %! @item freq
0031 %! Scalar integer, the frequency of the time series. @var{freq} is equal to 1 if data are on a yearly basis or if
0032 %! frequency is unspecified. @var{freq} is equal to 4 if data are on a quaterly basis. @var{freq} is equal to
0033 %! 12 if data are on a monthly basis. @var{freq} is equal to 52 if data are on a weekly basis.
0034 %! @item time
0035 %! Row vector of integers (1*2) indicating the year and the week, month or quarter of the first observation.
0036 %! @end table
0037 %! @sp 1
0038 %! @strong{This function is called by:}
0039 %! @sp 2
0040 %! @strong{This function calls:}
0041 %! @ref{set_time}
0042 %!
0043 %! @end deftypefn
0044 %@eod:
0045 
0046 % Copyright (C) 2011 Dynare Team
0047 % stephane DOT adjemian AT univ DASH lemans DOT fr
0048 %
0049 % This file is part of Dynare.
0050 %
0051 % Dynare is free software: you can redistribute it and/or modify
0052 % it under the terms of the GNU General Public License as published by
0053 % the Free Software Foundation, either version 3 of the License, or
0054 % (at your option) any later version.
0055 %
0056 % Dynare is distributed in the hope that it will be useful,
0057 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0058 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0059 % GNU General Public License for more details.
0060 %
0061 % You should have received a copy of the GNU General Public License
0062 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0063 
0064 date = struct;
0065 
0066 date.freq = NaN;
0067 date.time = NaN(1,2);
0068 
0069 date = class(date,'dynDate');
0070 
0071 switch nargin
0072   case 0
0073     return
0074   case 1
0075     if ischar(a)% Weekly, Monthly or Quaterly data.
0076         quaterly = findstr('Q',a);
0077         monthly  = findstr('M',a);
0078         weekly   = findstr('W',a);
0079         if ~isempty(quaterly)
0080             date.freq = 4;
0081             date.time(1) = str2num(a(1:quaterly-1));
0082             date.time(2) = str2num(a(quaterly+1:end));
0083         end
0084         if ~isempty(monthly)
0085             date.freq = 12;
0086             date.time(1) = str2num(a(1:monthly-1));
0087             date.time(2) = str2num(a(monthly+1:end));
0088         end
0089         if ~isempty(weekly)
0090             date.freq = 52;
0091             date.time(1) = str2num(a(1:weekly-1));
0092             date.time(2) = str2num(a(weekly+1:end));
0093         end
0094         if isempty(quaterly) && isempty(monthly) && isempty(weekly)
0095             if any(isletter(a))
0096                 error('dynDate:: Using a string as an input argument, I can only handle weekly (W), monthly (M) or quaterly (Q) data!');
0097             else
0098                 % Yearly data declared with a string
0099                 date.freq = 1;
0100                 date.time(1) = str2num(a);
0101                 date.time(2) = 1;
0102             end
0103         end
0104     elseif isa(a,'dynDate') % If input argument is a dynDate object then do a copy.
0105         date = a;
0106     else% If b is not a string then yearly data are assumed.
0107         date.freq = 1;
0108         date.time(1) = a;
0109         date.time(2) = 1;
0110     end
0111   otherwise
0112     error('dynDate:: Can''t instantiate the class, wrong calling sequence!')
0113 end
0114 
0115 %@test:1
0116 %$ addpath ../matlab
0117 %$
0118 %$ % Define some dates
0119 %$ date_1 = 1950;
0120 %$ date_2 = '1950Q2';
0121 %$ date_3 = '1950M10';
0122 %$ date_4 = '1950W50';
0123 %$ date_5 = '1950';
0124 %$
0125 %$ % Define expected results.
0126 %$ e_date_1 = [1950 1];
0127 %$ e_freq_1 = 1;
0128 %$ e_date_2 = [1950 2];
0129 %$ e_freq_2 = 4;
0130 %$ e_date_3 = [1950 10];
0131 %$ e_freq_3 = 12;
0132 %$ e_date_4 = [1950 50];
0133 %$ e_freq_4 = 52;
0134 %$ e_date_5 = [1950 1];
0135 %$ e_freq_5 = 1;
0136 %$
0137 %$ % Call the tested routine.
0138 %$ d1 = dynDate(date_1);
0139 %$ d2 = dynDate(date_2);
0140 %$ d3 = dynDate(date_3);
0141 %$ d4 = dynDate(date_4);
0142 %$ d5 = dynDate(date_5);
0143 %$
0144 %$ % Check the results.
0145 %$ t(1) = dyn_assert(d1.time,e_date_1);
0146 %$ t(2) = dyn_assert(d2.time,e_date_2);
0147 %$ t(3) = dyn_assert(d3.time,e_date_3);
0148 %$ t(4) = dyn_assert(d4.time,e_date_4);
0149 %$ t(5) = dyn_assert(d5.time,e_date_5);
0150 %$ t(6) = dyn_assert(d1.freq,e_freq_1);
0151 %$ t(7) = dyn_assert(d2.freq,e_freq_2);
0152 %$ t(8) = dyn_assert(d3.freq,e_freq_3);
0153 %$ t(9) = dyn_assert(d4.freq,e_freq_4);
0154 %$ t(10)= dyn_assert(d5.freq,e_freq_5);
0155 %$ T = all(t);
0156 %@eof:1

Generated on Fri 18-May-2012 02:41:00 by m2html © 2005