%% Read Spreadsheet Data into Table
% The best way to represent spreadsheet data in MATLAB® is in a table, which can store a mix of numeric and text data, as well as variable and row names. You can read data into tables interactively or programmatically. To interactively select data, click Import Data on the Home tab, in the Variable section. To programmatically import data, use one of these functions:
% readtable — Read a single worksheet.
% spreadsheetDatastore — Read multiple worksheets or files.
% This example shows how to import spreadsheet data programmatically using both functions. The sample data, airlinesmall_subset.xlsx, contains one sheet for each year between 1996 and 2008. The sheet names correspond to the year, such as 2003.
%% Read All Data from Worksheet
% Call readtable to read all the data in the worksheet called 2008, and then display only the first 10 rows and columns. Specify the worksheet name using the Sheet name-value pair argument. If your data is on the first worksheet in the file, you do not need to specify Sheet.

T = readtable('airlinesmall_subset.xlsx','Sheet','2008');
T(1:10,1:10)

%% Read Selected Range from Specific Worksheet
% From the worksheet named 1996, read only 10 rows of data from the first 5 columns by specifying a range, 'A1:E11'. The readtable function returns a 10-by-5 table.

T_selected = readtable('airlinesmall_subset.xlsx','Sheet','1996','Range','A1:E11')

%% Convert Variables to Datetimes, Durations, or Categoricals
% During the import process, readtable automatically detects the data types of the variables. However, if your data contains nonstandard dates, durations, or repeated labels, then you can convert those variable to their correct data types. Converting variables to their correct data types lets you perform efficient computations and comparisons and improves memory usage. For instance, represent the variables Year, Month, and DayofMonth as one datetime variable, the UniqueCarrier as categorical, and ArrDelay as duration in minutes.

data = T(:,{'Year','Month','DayofMonth','UniqueCarrier','ArrDelay'});
data.Date = datetime(data.Year,data.Month,data.DayofMonth);
data.UniqueCarrier = categorical(data.UniqueCarrier);
data.ArrDelay = minutes(data.ArrDelay);

% Find the day of the year with the longest delay, and then display the date.

ind = find(data.ArrDelay == max(data.ArrDelay));
data.Date(ind)

%% Read All Worksheets from Spreadsheet File
% A datastore is useful for processing arbitrarily large amounts of data that are spread across multiple worksheets or multiple spreadsheet files. You can perform data import and data processing through the datastore.
% Create a datastore from the collection of worksheets in airlinesmall_subset.xlsx, select the variables to import, and then preview the data.

ds = spreadsheetDatastore('airlinesmall_subset.xlsx');
ds.SelectedVariableNames = {'Year','Month','DayofMonth','UniqueCarrier','ArrDelay'};
preview(ds)

% Before importing data, you can specify what data types to use. For this example, import UniqueCarrier as a categorical variable.

 ds.SelectedVariableTypes(4) = {'categorical'};

% Import data using the readall or read functions. The readall function requires that all the data fit into memory, which is true for the sample data. After the import, compute the maximum arrival delay for this dataset.

alldata = readall(ds);
max(alldata.ArrDelay)/60

% For large data sets, import portions of the file using the read function. For more information, see Read Collection or Sequence of Spreadsheet Files.
% Copyright 2012 The MathWorks, Inc.