%% Import data from spreadsheet
% Script for importing data from the following spreadsheet:
%
%    Workbook: /Users/gt/Dropbox/KSC_rocket_launches_2016_onwards_v3.xlsx
%    Worksheet: Sheet1
%
% To extend the code for use with different selected data or a different
% spreadsheet, generate a function instead of a script.

% Auto-generated by MATLAB on 2017/11/27 16:46:08

%% Import the data
[~, ~, raw] = xlsread('/Users/gt/Dropbox/KSC_rocket_launches_2016_onwards_v3.xlsx','Sheet1');
raw = raw(2:end,:);
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[3,4,5,8,13]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,6,7,10,11,12,13]);

%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells

%% Create output variable
data = reshape([raw{:}],size(raw));

%% Create table
cols = table;

%% Allocate imported array to column variable names
cols.DATE = data(:,1);
cols.TIME = data(:,2);
cols.COMPANY = categorical(stringVectors(:,1));
cols.ROCKETTYPE = categorical(stringVectors(:,2));
cols.PAYLOAD = stringVectors(:,3);
cols.MASSt = data(:,3);
cols.SLC = data(:,4);
cols.ORBIT = categorical(stringVectors(:,4));
cols.RECORDED = data(:,5);
cols.SPACEX = data(:,6);
cols.ALL = data(:,7);
cols.OTHER = data(:,8);


%% Clear temporary variables
clearvars data raw stringVectors R;

%% plot
dnum=cols.DATE-1 + datenum(1899,12,31) + cols.TIME;
figure
plot(dnum,cols.ALL,'b^-',dnum,cols.SPACEX,'g^-',dnum,cols.RECORDED,'r^-',dnum,cols.OTHER,'r*-');
datetick('x','keeplimits')
ylabel('Cumulative number')
xlabel('Date')
legend('Launches - any', 'Launches - SpaceX', 'Launches recorded', 'Other events recorded')