%% Create and Modify Timetable Properties
% This example shows how to create and modify timetable properties. Meant only to provide the examplesectionobjects for the timetable properties page. Not meant to be published on its own.
%% Create First Timetable
% Create a timetable with default variable names. Then modify the names using the Properties.VariableNames property.

TT = timetable(datetime({'2015-12-18';'2015-12-19';'2015-12-20'}), ...
               [37.3;39.1;42.3],[30.1;30.03;29.9],[13.4;6.5;7.3])

%
%% Modify Variable Names

TT.Properties.VariableNames = {'Temp','Pressure','WindSpeed'}

% A fundamental way to display and modify variables is to access them by name using dot syntax.

TT.Temp
TT.Pressure(3) = 30

%
%% Subscript on Variable Names
% Create a subtable. Include all rows, but subscript on Temp and Pressure.

TT(:,{'Temp','Pressure'})

%
%% Extract Data with Subscripts
% Extract data from TT. Use variable names within curly braces to extract the data from the variables Pressure and WindSpeed.

TT{:,{'Pressure','WindSpeed'}}

%
%% Extract Data with Dot Indexing
% Extract data from the variable Temp using dot indexing.

TT.Temp

%
%% Extract All Data
% Extract all data using the Variables property. TT.Variables is equivalent to TT{:,:}.

TT.Variables

%
%% Create Second Timetable
% Create a timetable. Then replace its row times using the Properties.RowTimes property.

TT = timetable(datetime({'2015-12-18';'2015-12-19';'2015-12-20'}), ...
               [37.3;39.1;42.3],[30.1;30.03;29.9],[13.4;6.5;7.3])

%
%% Display Row Times

Dates = datetime(2017,1,1:3);
TT.Properties.RowTimes = Dates

% Another way to access the row times is by using dot syntax with the name of the first dimension of the timetable.

TT.Properties.DimensionNames
TT.Time

%
%% Display Row Times by Name
% Display row times by the name of the vector of row times.

TT.Time

%
%% Subscript by Row Times
% Create a subtable. Include all variables, but subscript on a subset of row times.

TT({'2017-01-01','2017-01-03'},:)

%
%% Modify Dimension Names
% Create a timetable and display its dimension names. You can access row times and data using dimension names with dot syntax.

TT = timetable(datetime({'2015-12-18';'2015-12-19';'2015-12-20'}), ...
               [37.3;39.1;42.3],[30.1;30.03;29.9],[13.4;6.5;7.3]);
TT.Properties.DimensionNames

% Access the row times using the first dimension name.

TT.Time

% Access the data using the second dimension name. This syntax is equivalent to TT{:,:,}.

TT.Variables

% Modify the names of its dimensions using the Properties.DimensionNames property. Having changed the dimension names, you can access the row times and data using the syntaxes TT.Date and TT.WeatherData respectively.

TT.Properties.DimensionNames = {'Date','WeatherData'};
TT.Properties

%
%% Modify Description
% Create a timetable. Modify the variable names and the description of the timetable. Display a summary of the result.

TT = timetable(datetime({'2015-12-18';'2015-12-19';'2015-12-20'}), ...
               [37.3;39.1;42.3],[30.1;30.03;29.9],[13.4;6.5;7.3]);
TT.Properties.VariableNames = {'Temp','Pressure','WindSpeed'};
TT.Properties.Description = 'Weather Data from December 2015';
summary(TT)

%
%% Modify Variable Descriptions
% Create a timetable. Modify the variable names and descriptions. Display a summary of the result.

TT = timetable(datetime({'2015-12-18';'2015-12-19';'2015-12-20'}), ...
               [37.3;39.1;42.3],[30.1;30.03;29.9],[13.4;6.5;7.3]);
TT.Properties.VariableNames = {'Temp','Pressure','WindSpeed'};
TT.Properties.VariableDescriptions = {'Temperature (external)', ...
                                      'Pressure in Hg', ...
                                      'Wind speed at sensor 123'};
summary(TT)

%
%% Modify Variable Units
% Create a timetable. Modify the variable names and units. Display a summary of the result.

TT = timetable(datetime({'2015-12-18';'2015-12-19';'2015-12-20'}), ...
               [37.3;39.1;42.3],[30.1;30.03;29.9],[13.4;6.5;7.3]);
TT.Properties.VariableNames = {'Temp','Pressure','WindSpeed'};
TT.Properties.VariableUnits = {'degrees F','mm Hg','mph'};
summary(TT)

%
%% Modify User Data
% Create a timetable. Modify the variable names. Attach an anonymous function as a piece of user data that is associated with the timetable.

TT = timetable(datetime({'2015-12-18';'2015-12-19';'2015-12-20'}), ...
               [37.3;39.1;42.3],[30.1;30.03;29.9],[13.4;6.5;7.3]);
TT.Properties.VariableNames = {'Temp','Pressure','WindSpeed'};
Fahrenheit2Celsius = @(x) (5.0/9.0).*(x - 32);
TT.Properties.UserData = Fahrenheit2Celsius;
TT.Properties

%
%% Modify VariableContinuity
% Create a timetable. Specify a matlab.tabular.Continuity value for each variable.

TT = timetable(datetime({'2015-12-18';'2015-12-19';'2015-12-20'}), ...
               [37.3;39.1;42.3],[13.4;6.5;7.3],{'N';'SE';'NW'});
TT.Properties.VariableNames = {'Temperature','WindSpeed','WindDirection'};
TT.Properties.VariableContinuity = {'continuous','event','event'};
TT.Properties

%
%% Modify StartTime
% Create a timetable. In this timetable, the time step between consecutive rows is not the same, so the timetable is irregular.

TT = timetable(datetime({'2015-12-18';'2015-12-20';'2015-12-21'}), ...
               [37.3;39.1;42.3],[13.4;6.5;7.3],{'N';'SE';'NW'});
TT.Properties.VariableNames = {'Temperature','WindSpeed','WindDirection'}

% Display its properties. The value of the StartTime property is equal to the first row time. Whether a timetable is regular or irregular, it always has a start time, unless it is empty.

TT.Properties

% Modify the StartTime property. Note that all the row times have new values. For each row time, the change in value is equal to the difference between the original value of the first row time and the new start time.

TT.Properties.StartTime = datetime('2018-04-09')

%
%% Modify SampleRate
% Create a regular timetable. In this timetable, the row times are durations, created using the same sample rate.

Intensity = [100;98.7;95.2;101.4;99.1];
TT = timetable(Intensity,'SampleRate',100)

% Display its properties. The SampleRate property stores the sample rate in Hz.

TT.Properties

% Modify the SampleRate property. The start time is the same, but all the other row times are different because the sample rate is smaller. However, the variable Intensity remains the same.

TT.Properties.SampleRate = 25

%
%% Modify TimeStep
% Create a regular timetable. In this timetable, the row times are durations, created using the same time step.

Intensity = [100;98.7;95.2;101.4;99.1];
TT = timetable(Intensity,'TimeStep',seconds(0.01))

% Display its properties. The TimeStep property stores the time step as a duration.

TT.Properties

% Modify the TimeStep property. The start time is the same, but all the other row times are different because the time step is larger. However, the variable Intensity remains the same.

TT.Properties.TimeStep = seconds(0.04)

%
%% Add Customized Metadata
% Create a timetable containing weather data.

TT = timetable(datetime({'2015-12-18';'2015-12-20';'2015-12-21'}), ...
               [37.3;39.1;42.3],[13.4;6.5;7.3],{'N';'SE';'NW'});
TT.Properties.VariableNames = {'Temperature','WindSpeed','WindDirection'}

% To describe the instruments that measured these data, and the name of an output file, add customized metadata using the addprop function. The Instruments property has variable metadata that apply to the variables of TT. The OutputFile property has table metadata.

TT = addprop(TT,{'Instruments','OutputFile'},{'variable','table'});
TT.Properties

% Assign values to the customized metadata using dot syntax. When you assign an array of text values to customized metadata, the best practice is to use a string array, not a cell array of character vectors. If a property of CustomProperties is a cell array of character vectors, then there is no mechanism to prevent you from later assigning nontext values as elements of the cell array.

TT.Properties.CustomProperties.Instruments = ["thermometer","anemometer","wind vane"];
TT.Properties.CustomProperties.OutputFile = 'weatherReadings.csv';
TT.Properties

%
%% Remove Customized Metadata
% Remove the OutputFile property from TT.

TT = rmprop(TT,'OutputFile');
TT.Properties

%
% Copyright 2018 The MathWorks, Inc.