%% Read in data from .xls files and save as .mat
% The following data files will be converted from .xls to .mat:
% - DataVulnerability.xls
% - DataVulnerabilityAppendix.xls
% The code below is platform-dependent due to differences in the behavior
% of xlsread on PC vs. Mac/Linux.
% 
% Downloaded from the replication files for:
% Tobias Adrian, Nina Boyarchenko, and Domenico Giannone (2018):
% "Vulnerable Growth," American Economic Review.
%% Clear workspace
clear
clc

%% Main data (GDP and NFCI)
xlsfilename = 'DataVulnerability.xls';
if ispc()
    [DATA, TEXT] = xlsread(xlsfilename, 'Data');
    Time = datenum(TEXT(2:end, 1));
    X = DATA;
    clear('DATA', 'TEXT')
else
    [DATA, TEXT] = xlsread(xlsfilename, 'Data');
    Time = DATA(:, 1) + datenum('12/30/1899', 'mm/dd/yyyy');
    X = DATA(:, 2:end);
    clear('DATA', 'TEXT')
end
clear('xlsfilename')
Mnem = {
    'A191RL1Q225SBEA'
    'NFCI'
    };
% Check data validity
assert(length(Time) == size(X, 1))
assert(length(Mnem) == size(X, 2))
% Save data as .mat
save('DataVulnerability.mat', 'X', 'Time', 'Mnem')
disp('Saved DataVulnerability.mat')
clear
    
%% Appendix/alternative measures data
xlsfilename = 'DataVulnerabilityAppendix.xls';
if ispc()
    [DATA,TEXT] = xlsread(xlsfilename, 'Data');
    Time = datenum(TEXT(2:end, 1));
    X = DATA;
    clear('DATA','TEXT')
else
    [DATA,TEXT] = xlsread(xlsfilename, 'Data');
    Time = DATA(:,1) + datenum('12/30/1899','mm/dd/yyyy');
    X = DATA(:, 2:end);
    clear('DATA', 'TEXT')
end
Mnem = {
    'A191RL1Q225SBEA'
    'NFCI'
    'NFCILEVERAGE'
    'NFCICREDIT'
    'NFCINONFINLEVERAGE'
    'NFCIRISK'
    'VXO'
    'CredSpread'
    'TermSpread'
    };
% Check data validity
assert(length(Time) == size(X, 1))
assert(length(Mnem) == size(X, 2))
% Save data
save('DataVulnerabilityAppendix.mat', 'X', 'Time', 'Mnem')
disp('Saved DataVulnerabilityAppendix.mat')
clear
