% Set-up for gplotmatrix of annualized S&P500 and NASDAQ data.
% Converts the vectors "S" and "N" in SNindices.mat to matrix form, 
% with each column containing date-aligned available data for a 
% different year. Missing dates are padded with NaNs.

load SNindices

% SNindices contains a vector "dates" of corresponding date strings.
% Create a group based on year:
ygroup = dates(:,8:11);
    
% Extract indices for year groups in "dates":
I98 = ismember(cellstr(ygroup), '1998');
I99 = ismember(cellstr(ygroup), '1999');
I00 = ismember(cellstr(ygroup), '2000');
I01 = ismember(cellstr(ygroup), '2001');

% Create a *complete* list of date strings for each year:
D98 = datestr(datenum('01-Jan-1998'):datenum('31-Dec-1998'));
D99 = datestr(datenum('01-Jan-1999'):datenum('31-Dec-1999'));
D00 = datestr(datenum('01-Jan-2000'):datenum('31-Dec-2000'));
D01 = datestr(datenum('01-Jan-2001'):datenum('31-Dec-2001'));

% Add 29-Feb for non-leap years to make lists commensurate:
n = datenum('28-Feb-1998') - datenum('01-Jan-1998');

if eomday(1998,2) == 28
    d98(1:n+1,:) = D98(1:n+1,:);
    d98(n+2,:) = '29-Feb-1998';
    d98(n+3:366,:) = D98(n+2:365,:);
    D98 = d98;
    clear d98
end
   
if eomday(1999,2) == 28
    d99(1:n+1,:) = D99(1:n+1,:);
    d99(n+2,:) = '29-Feb-1999';
    d99(n+3:366,:) = D99(n+2:365,:);
    D99 = d99;
    clear d99
end

if eomday(2000,2) == 28
    d00(1:n+1,:) = D00(1:n+1,:);
    d00(n+2,:) = '29-Feb-2000';
    d00(n+3:366,:) = D00(n+2:365,:);
    D00 = d00;
    clear d00
end

if eomday(2001,2) == 28
    d01(1:n+1,:) = D01(1:n+1,:);
    d01(n+2,:) = '29-Feb-2001';
    d01(n+3:366,:) = D01(n+2:365,:);
    D01 = d01;
    clear d01
end

clear n

% Align available data in commensurate 366x1 vectors.
% Unavailable data is replaced by NaNs:
S98 = repmat([NaN],366,1);
J98 = ismember(cellstr(D98),cellstr(dates(I98,:)));
S98(J98) = S(I98);

S99 = repmat([NaN],366,1);
J99 = ismember(cellstr(D99),cellstr(dates(I99,:)));
S99(J99) = S(I99);

S00 = repmat([NaN],366,1);
J00 = ismember(cellstr(D00),cellstr(dates(I00,:)));
S00(J00) = S(I00);

S01 = repmat([NaN],366,1);
J01 = ismember(cellstr(D01),cellstr(dates(I01,:)));
S01(J01) = S(I01);

N98 = repmat([NaN],366,1);
J98 = ismember(cellstr(D98),cellstr(dates(I98,:)));
N98(J98) = N(I98);

N99 = repmat([NaN],366,1);
J99 = ismember(cellstr(D99),cellstr(dates(I99,:)));
N99(J99) = N(I99);

N00 = repmat([NaN],366,1);
J00 = ismember(cellstr(D00),cellstr(dates(I00,:)));
N00(J00) = N(I00);

N01 = repmat([NaN],366,1);
J01 = ismember(cellstr(D01),cellstr(dates(I01,:)));
N01(J01) = N(I01);

clear I98 J98 I99 J99 I00 J00 I01 J01

% Assemble 366x4 matrices with annual data by column:
yS = [S98,S99,S00,S01];
yN = [N98,N99,N00,N01];

clear S98 N98 S99 N99 S00 N00 S01 N01

% Create a commensurate group vector based on month:
mgroup = D98(:,4:6);

clear D98 D99 D00 D01

% Prepare inputs for gplotmatrix:
Snames = ['S&P500 1998';
                 'S&P500 1999';
                 'S&P500 2000';
                 'S&P500 2001'];
             
Nnames = ['NASDAQ 1998';
                 'NASDAQ 1999';
                 'NASDAQ 2000';
                 'NASDAQ 2001'];
             
colors = 'rgbcmyk';

% Plot:
gplotmatrix(yS,yN,mgroup,colors,'*',3*ones(12,1),'off',[],Snames,Nnames)

clear colors Snames Nnames