This example shows how to add custom properties to tables and timetables, set and access their values, and remove them.
All tables and timetables have properties that contain metadata about them or their variables. You can access these properties through theT
.Properties
object, whereT
is the name of the table or timetable. For example,T
.Properties.VariableNames
returns a cell array containing the names of the variables ofT
.
The properties you access throughT
.Properties
are part of the definitions of thetable
andtimetable
data types. You cannot add or remove these predefined properties. But starting in R2018b, you can add and remove your owncustomproperties, by modifying theT
.Properties.CustomProperties
object of a table or timetable.
Read power outage data into a table. Sort it using the first variable that contains dates and times,OutageTime
. Then display the first three rows.
T = readtable('outages.csv'); T = sortrows(T,'OutageTime'); head(T,3)
ans=3×6 tableRegion OutageTime Loss Customers RestorationTime Cause _____________ ________________ ______ __________ ________________ ________________ {'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm'} {'MidWest' } 2002-03-05 17:53 96.563 2.8666e+05 2002-03-10 14:41 {'wind' } {'MidWest' } 2002-03-16 06:18 186.44 2.1275e+05 2002-03-18 23:23 {'severe storm'}
Display its properties. These are the properties that all tables have in common. Note that there is also aCustomProperties
object, but that by default it has no properties.
T.Properties
ans = TableProperties属性:描述: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {1x6 cell} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.
要添加自定义属性,请使用addprop
function. Specify the names of the properties. For each property, also specify whether it has metadata for the whole table (similar to theDescription
property) or for its variables (similar to theVariableNames
属性)。如果属性有variable metadata, then its value must be a vector whose length is equal to the number of variables.
Add custom properties that contain an output file name, file type, and indicators of which variables to plot. Best practice is to assign the input table as the output argument ofaddprop
, so that the custom properties are part of the same table. Specify that the output file name and file type are table metadata using the'table'
option. Specify that the plot indicators are variable metadata using the'variable'
option.
T = addprop(T,{'OutputFileName','OutputFileType','ToPlot'},...{'table','table','variable'}); T.Properties
ans = TableProperties属性:描述: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {1x6 cell} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} Custom Properties (access using t.Properties.CustomProperties.): OutputFileName: [] OutputFileType: [] ToPlot: []
When you add custom properties usingaddprop
, their values are empty arrays by default. You can set and access the values of the custom properties using dot syntax.
Set the output file name and type. These properties contain metadata for the table. Then assign a logical array to theToPlot
property. This property contains metadata for the variables. In this example, the elements of the value of theToPlot
property aretrue
for each variable to be included in a plot, andfalse
for each variable to be excluded.
t.properties.customproperties.outputfilename ='outageResults';t.properties.customproperties.outputfiletype ='.mat';T.Properties.CustomProperties.ToPlot = [false false true true true false]; T.Properties
ans = TableProperties属性:描述: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {1x6 cell} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} Custom Properties (access using t.Properties.CustomProperties.): OutputFileName: 'outageResults' OutputFileType: '.mat' ToPlot: [0 0 1 1 1 0]
Plot variables fromT
in a stacked plot using thestackedplot
function. To plot only theLoss
,Customers
, andRestorationTime
values, use theToPlot
custom property as the second input argument.
stackedplot(T,T.Properties.CustomProperties.ToPlot);
When you move or delete table variables, both the predefined and custom properties are reordered so that their values correspond to the same variables. In this example, the values of theToPlot
custom property stay aligned with the variables marked for plotting, just as the values of theVariableNames
predefined property stay aligned.
Remove theCustomers
variable and display the properties.
T.Customers = []; T.Properties
ans = TableProperties属性:描述: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {1x5 cell} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} Custom Properties (access using t.Properties.CustomProperties.): OutputFileName: 'outageResults' OutputFileType: '.mat' ToPlot: [0 0 1 1 0]
Convert the table to a timetable, using the outage times as row times. MoveRegion
to the end of the table, andRestorationTime
before the first variable, using themovevars
function. Note that the properties are reordered appropriately. TheRestorationTime
andLoss
variables still have indicators for inclusion in a plot.
T = table2timetable(T); T = movevars(T,'Region','After','Cause'); T = movevars(T,'RestorationTime','Before',1); T.Properties
ans = TimetableProperties with properties: Description: '' UserData: [] DimensionNames: {'OutageTime' 'Variables'} VariableNames: {'RestorationTime' 'Loss' 'Cause' 'Region'} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowTimes: [1468x1 datetime] StartTime: 2002-02-01 12:18 SampleRate: NaN TimeStep: NaN Custom Properties (access using t.Properties.CustomProperties.): OutputFileName: 'outageResults' OutputFileType: '.mat' ToPlot: [1 1 0 0]
You can remove any or all of the custom properties of a table using thermprop
function. However, you cannot use it to remove predefined properties fromT.Properties
, because those properties are part of the definition of thetable
data type.
Remove theOutputFileName
andOutputFileType
custom properties. Display the remaining table properties.
T = rmprop(T,{'OutputFileName','OutputFileType'}); T.Properties
ans = TimetableProperties with properties: Description: '' UserData: [] DimensionNames: {'OutageTime' 'Variables'} VariableNames: {'RestorationTime' 'Loss' 'Cause' 'Region'} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowTimes: [1468x1 datetime] StartTime: 2002-02-01 12:18 SampleRate: NaN TimeStep: NaN Custom Properties (access using t.Properties.CustomProperties.): ToPlot: [1 1 0 0]
readtable
|table
|head
|addprop
|table2timetable
|movevars
|rmprop
|sortrows
|stackedplot