%% Use Interactive Controls in a Live Script
% This example shows how you can add interactive controls to a live script. Adding interactive controls to a live script is useful when you want to share the live script with others. Use interactive controls to set and change the values of variables in your live script using familiar user interface components such as numeric sliders and drop-down lists.
%% Add Interactive Controls to Your Script
% To add an interactive control, select a value in the live script. Then, go to the Insert tab, click the Control button, and select either Numeric Slider or Drop Down. To configure the interactive control, double-click it. When you change the value of an interactive control, the Live Editor runs the section that contains the control.
%% Visualize Airport and Carrier Delays
% Use the interactive controls in the live script to set the type of delay, the year, and the number of airports and carriers to visualize. The live script reads in the airport delay data using the readDelays function. The calculateDelays function calculates the average delay for the top airports and the top carriers. The plotDelays function creates bar charts to display the delays for the top airports and top carriers.

delayType   = 'DepDelay';
year        =  1999;
topAirports =  7;
topCarriers =  10;

allDelays = readDelays;
[airportDelays, carrierDelays] = calculateDelays(allDelays, year, topAirports, topCarriers);
plotDelays(year, delayType, airportDelays, carrierDelays)

%
% Read in the airport delay data.

function allDelays = readDelays

isRead = evalin('base','exist(''allDelays'')');
if ~isRead
    opts = detectImportOptions('airlinesmall.csv');
    allDelays = readtable('airlinesmall.csv',opts);
    allDelays(allDelays.Year==1987,:) = [];
    allDelays.UniqueCarrier = categorical(allDelays.UniqueCarrier);
    allDelays.Origin = categorical(allDelays.Origin);
else
    allDelays = evalin('base','allDelays');
end

end

%
% Calculate the average delay for the top airports and the top carriers.

function [airportDelays, carrierDelays] = calculateDelays(allDelays, year, topAirports, topCarriers)

yearDelays = allDelays(allDelays.Year==year,:);
carriers = groupsummary(yearDelays,'UniqueCarrier');
carriers = sortrows(carriers,'GroupCount','descend');
airports = groupsummary(yearDelays,'Origin');
airports = sortrows(airports,'GroupCount','descend');

numAirports = min(topAirports,size(airports,1));
numCarriers = min(topCarriers,size(carriers,1));

airportDelays = yearDelays(ismember(yearDelays.Origin,airports{1:numAirports,'Origin'}),:);
airportDelays.Origin = removecats(airportDelays.Origin);
carrierDelays = yearDelays(ismember(yearDelays.UniqueCarrier,carriers{1:numCarriers,'UniqueCarrier'}),:);
carrierDelays.UniqueCarrier = removecats(carrierDelays.UniqueCarrier);

end

%
% Create bar charts to display the delays for the top airports and top carriers.

function plotDelays(year, delayType, airportDelays, carrierDelays)

figure('Position',[680 678 950 320])
subplot(1,2,1)
meanAirportDelays = groupsummary(airportDelays,'Origin','mean',delayType);
meanAirportDelays.Properties.VariableNames{3} = erase(meanAirportDelays.Properties.VariableNames{3},'mean_');
bar(categorical(meanAirportDelays.Origin),meanAirportDelays{:,delayType})
xlabel('Airport')
ylabel('Average Delay (minutes)')
title(['Average ' delayType ' by Airport in ' num2str(year)])

subplot(1,2,2)
meanCarrierDelays = groupsummary(carrierDelays,'UniqueCarrier','mean',delayType);
meanCarrierDelays.Properties.VariableNames{3} = erase(meanCarrierDelays.Properties.VariableNames{3},'mean_');
bar(categorical(meanCarrierDelays.UniqueCarrier),meanCarrierDelays{:,delayType})
xlabel('Carrier')
ylabel('Average Delay (minutes)')
title(['Average ' delayType ' by Carrier in ' num2str(year)])

end