%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%             MASTER CODE: FERNÁNDEZ, REBUCCI AND URIBE (2015)           %
%             -----------------------------------------------            %
%                  "Are Capital Controls Countercyclical?"               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% This is the user-oriented interface code for the paper 
% "Are Capital Controls Countercyclical?" by Andrés Fernández (IDB), 
% Alessandro Rebucci (Johns Hopkins) and Martín Uribe (Columbia and NBER) (2015), 
% published in the Journal of Monetary Economics, 76, 1-14.

% INSTRUCTIONS: You will only need to run this code (keyboard shortcut: F5). 
% Once this is done, the user will be prompted to select from two windows, first, what type of 
% operating system is it being used (Windows or Mac), anf then select the table/figure 
% of your interest, either from the published article or the Online Appendix. 

% Note that some figures might take up several minutes to appear 
% (in particular, Figures 5.A.1 through 5.A.8). Be aware that some versions
% of Excel may pop windows asking you to save changes while running certain figures, 
% if so, please select "No".

% Authors: Javier Caicedo (caicedo.javier@javeriana.edu.co) 
%          PEC - Inter-American Development Bank
%          Andrés Fernández (andresf@iadb.org)
%          Economist - Inter-American Development Bank
% Date:    September, 2015

% Research assistance in earlier drafts was provided by
% Luca Mazzone and Umberto Muratori.

% For further detail on each file of this zip folder, please
% refer to the README file (available in .pdf)

% Should you have any questions, contact us at andresf@iadb.org.

%%
%%%%%%%%%%%%%%%%%%%%%%%
% USER INTERFACE CODE %
%%%%%%%%%%%%%%%%%%%%%%%

clear all
close all
clc

% Creating the list of tables and figures of the core paper and in the
% online appendix.

str2= {'Windows user','Mac user'};

[mac_user,~] = listdlg('PromptString','Select your operating system:',...
                'SelectionMode','single','ListString',str2,...
                'Name','Fernández, Rebucci and Uribe (2015)',...
                'ListSize',[200 200]);

str = {'---ARTICLE---','Table  1','Figure 1, Figure 2',...
       'Figure 3','Figure 4','Figure 5',...
       'Figure 6','Figure 7','Figure 8','Figure 9','---ONLINE APPENDIX---',...
       'Table A.2','Table A.3.a','Table A.3.b','Figure 3.A.1',...
       'Figure A.2','Figure A.3','Figure 4.A.1, Figure 4.A.2, Figure 4.A.3' ,...
       'Figure A.5.1, Figure A.5.2, Figure A.5.3',...
       'Figure A.6.1, Figure A.6.2' ,'Figure A.6.3, Figure A.6.4',...
       'Figure A.7','Figure 1.A.1, Figure 1.A.2, Figure 2.A.1, Figure 2.A.2',...
       'Figure 5.A.1, Figure 5.A.2, Figure 5.A.3, Figure 5.A.4',...
       'Figure 5.A.5, Figure 5.A.6, Figure 5.A.7, Figure 5.A.8',...
       'Figure 6.A.1, Figure 6.A.2','Figure 6.A.3, Figure 6.A.4',...
       'Figure 7.A.1' ,'Figure 7.A.2','Figure 9.A.1, Figure 9.A.2'};
   
% Creating a listbox.
   
[s,v] = listdlg('PromptString','Select the Table or Figure(s):',...
                'SelectionMode','single','ListString',str,...
                'Name','Fernández, Rebucci and Uribe (2015)',...
                'ListSize',[300 300]);

% If no selection is made, a warning is shown on the command window.            
            
if v==0
    display('Please run again the code and select the Table or Figure(s) of your interest.')
    commandwindow
    break
end

% These are the codes 
            
gdp_script    =dir('Main_script_GDP.m');
reer_script   =dir('Main_script_REER.m');
chinn_script  =dir('Main_script_CHINN.m');
growth_script =dir('Main_script_growth.m');
ca_script     =dir('Main_script_CA.m');

% This section is intended to call the necessary code to produce all
% figures, as presented in the listbox.
            
switch s
% These are the figures and tables depicted in the CORE PAPER.
    case 2 
       % Table  1
       variable=6;
       run(fullfile(cd,gdp_script.name));
       Table1=Table_two_paper;
       clc
       display(Table1)
       commandwindow
    case 3
       % Figure 1 and Figure 2
       set(0,'DefaultFigureVisible','on');
       variable=4;
       run(fullfile(cd,gdp_script.name));
       clc
    case 4
       % Figure 3
       set(0,'DefaultFigureVisible','on');
       variable=8;
       run(fullfile(cd,gdp_script.name)); 
       clc
    case 5
       % Figure 4
       set(0,'DefaultFigureVisible','on');
       variable=31;
       run(fullfile(cd,gdp_script.name));
       clc
    case 6
       % Figure 5
       set(0,'DefaultFigureVisible','on');
       variable=13;
       run(fullfile(cd,gdp_script.name));
       clc
    case 7
       % Figure 6
       set(0,'DefaultFigureVisible','on');
       variable=7;
       run(fullfile(cd,reer_script.name)); 
       clc
    case 8
       % Figure 7
       set(0,'DefaultFigureVisible','on');
       variable=16;
       run(fullfile(cd,gdp_script.name));
       clc
    case 9
       % Figure 8
       set(0,'DefaultFigureVisible','on');
       variable=18;
       run(fullfile(cd,gdp_script.name)); 
       clc
    case 10
       % Figure 9
       set(0,'DefaultFigureVisible','on');
       variable=6;
       run(fullfile(cd,chinn_script.name)); 
       clc
% These are the figures and tables shown on the ONLINE APPENDIX. 
    case 12
       % Table A.2
       variable=6;
       set(0,'DefaultFigureVisible','off');
       run(fullfile(cd,gdp_script.name));
       TableA2=Table_average_corr_inflow1_outflow1;
       clc
       display(TableA2)
       commandwindow
    case 13
       % Table A.3.a
       variable=4;
       set(0,'DefaultFigureVisible','off');
       run(fullfile(cd,gdp_script.name));
       TableA3a=Table_inflow_corr;
       clc
       display(TableA3a)
       commandwindow
    case 14
       % Table A.3.b
       variable=4;
       set(0,'DefaultFigureVisible','off');
       run(fullfile(cd,gdp_script.name));
       TableA3b=Table_outflow_corr;
       clc
       display(TableA3b)
       commandwindow
    case 15
       % Figure 3.A.1
       set(0,'DefaultFigureVisible','on');
       variable=25;
       run(fullfile(cd,gdp_script.name)); 
       clc
    case 16
       % Figure A.2
       set(0,'DefaultFigureVisible','on');
       variable=15;
       run(fullfile(cd,gdp_script.name)); 
       clc
    case 17
       % Figure A.3
       set(0,'DefaultFigureVisible','on');
       variable=2;
       run(fullfile(cd,growth_script.name)); 
       clc
    case 18
       % Figures 4.A.1, 4.A.2, 4.A.3
       set(0,'DefaultFigureVisible','on');
       variable=22;
       run(fullfile(cd,gdp_script.name)); 
       clc
    case 19
       % Figures A.5.1, A.5.2, A.5.3
       set(0,'DefaultFigureVisible','on');
       variable=21;
       run(fullfile(cd,gdp_script.name)); 
       clc
    case 20
       % Figures A.6.1, A.6.2
       set(0,'DefaultFigureVisible','on');
       variable=6;
       run(fullfile(cd,reer_script.name)); 
       clc
    case 21
       % Figures A.6.3, A.6.4
       set(0,'DefaultFigureVisible','on');
       variable=6;
       run(fullfile(cd,ca_script.name)); 
       clc
    case 22
       % Figure A.7
       set(0,'DefaultFigureVisible','on');
       variable=8;
       run(fullfile(cd,chinn_script.name)); 
       clc
    case 23
       % Figures 1.A.1, 1.A.2, 2.A.1, 2.A.2
       set(0,'DefaultFigureVisible','on');
       variable=24;
       run(fullfile(cd,gdp_script.name)); 
       clc
    case 24
       % Figures 5.A.1, 5.A.2, 5.A.3, 5.A.4
       set(0,'DefaultFigureVisible','on');
       variable=27;
       run(fullfile(cd,gdp_script.name)); 
       clc
    case 25
       % Figures 5.A.5, 5.A.6, 5.A.7, 5.A.8
       set(0,'DefaultFigureVisible','on');
       variable=26;
       run(fullfile(cd,gdp_script.name)); 
       clc
    case 26
       % Figures 6.A.1, 6.A.2
       set(0,'DefaultFigureVisible','on');
       variable=10;
       run(fullfile(cd,reer_script.name)); 
       clc
    case 27
       % Figures 6.A.3, 6.A.4
       set(0,'DefaultFigureVisible','on');
       variable=9;
       run(fullfile(cd,reer_script.name)); 
       clc
    case 28
       % Figure 7.A.1
       set(0,'DefaultFigureVisible','on');
       variable=29;
       run(fullfile(cd,gdp_script.name)); 
       clc
    case 29
       % Figure 7.A.2
       set(0,'DefaultFigureVisible','on');
       variable=28;
       run(fullfile(cd,gdp_script.name)); 
       clc
    case 30
       % Figures 9.A.1, 9.A.2
       set(0,'DefaultFigureVisible','on');
       variable=7;
       run(fullfile(cd,chinn_script.name)); 
       clc
end
            
clear s str gdp_script reer_script chinn_script growth_script ca_script