Main Content

Add Custom Properties to Tables and Timetables

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.Propertiesobject, whereTis the name of the table or timetable. For example,T.Properties.VariableNamesreturns a cell array containing the names of the variables ofT.

The properties you access throughT.Propertiesare part of the definitions of thetableandtimetabledata types. You cannot add or remove these predefined properties. But starting in R2018b, you can add and remove your owncustomproperties, by modifying theT.Properties.CustomPropertiesobject of a table or timetable.

Add Properties

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 aCustomPropertiesobject, 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.

要添加自定义属性,请使用addpropfunction. Specify the names of the properties. For each property, also specify whether it has metadata for the whole table (similar to theDescriptionproperty) 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: []

Set and Access Values of Custom Properties

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 theToPlotproperty. This property contains metadata for the variables. In this example, the elements of the value of theToPlotproperty aretruefor each variable to be included in a plot, andfalsefor 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 fromTin a stacked plot using thestackedplotfunction. To plot only theLoss,Customers, andRestorationTimevalues, use theToPlotcustom property as the second input argument.

stackedplot(T,T.Properties.CustomProperties.ToPlot);

Figure contains an object of type stackedplot.

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 theToPlotcustom property stay aligned with the variables marked for plotting, just as the values of theVariableNamespredefined property stay aligned.

Remove theCustomersvariable 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. MoveRegionto the end of the table, andRestorationTimebefore the first variable, using themovevarsfunction. Note that the properties are reordered appropriately. TheRestorationTimeandLossvariables 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]

Remove Properties

You can remove any or all of the custom properties of a table using thermpropfunction. However, you cannot use it to remove predefined properties fromT.Properties, because those properties are part of the definition of thetabledata type.

Remove theOutputFileNameandOutputFileTypecustom 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]

See Also

||||||||

Related Topics