此示例演示如何向表和时间表添加自定义属性、设置和访问它们的值以及删除它们。
所有表和时间表都具有包含关于它们或它们的变量的元数据的属性。方法访问这些属性T
. properties
对象,T
是表格或时间表的名称。例如,T
.Properties.VariableNames
的变量名返回单元格数组T
.
您访问的属性T
. properties
的部分定义是表格
而且时间表
数据类型。不能添加或删除这些预定义属性。但是从R2018b开始,您可以添加和删除您自己的自定义属性,通过修改T
.Properties.CustomProperties
表或时间表的对象。
将停电数据读入表。使用第一个包含日期和时间的变量对其排序,OutageTime
.然后显示前三行。
T =可读的(“outages.csv”);T = sortrows(T,“OutageTime”);头(T, 3)
ans =3×6表地区OutageTime RestorationTime造成损失客户 _____________ ________________ ______ __________ ________________ ________________ {' 西南的}2002-02-01 12:18 458.98 - 1.8202 e + 06 2002-02-01 16:50{“暴风雪”}{“中西部”}2002-03-05 17:53 96.563 - 2.8666 e + 05 2002-03-05 41{‘风’}{“中西部”}2002-03-16 06:18 186.44 - 2.1275 e + 05 2002-03-16 23:23{“暴风”}
显示其属性。这些是所有表共有的属性。注意这里还有一个CustomProperties
对象,但默认情况下它没有属性。
T.Properties
ans = TableProperties带有属性:描述:" UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {1x6 cell} variabledescription: {} VariableUnits: {} variableccontinucontinuity: [] RowNames: {} CustomProperties:没有自定义属性设置。使用addprop和rmprop修改CustomProperties。
要添加自定义属性,请使用addprop
函数。指定属性的名称。对于每个属性,还要指定它是否具有整个表的元数据(类似于描述
属性)或用于其变量(类似于VariableNames
属性)。如果属性具有变量元数据,则其值必须是长度等于变量数量的向量。
添加自定义属性,其中包含输出文件名、文件类型和要绘制的变量的指示器。的输出参数指定输入表为最佳实践addprop
,以便自定义属性是同一个表的一部分。属性指定输出文件名和文件类型为表元数据“表”
选择。属性指定绘图指示器为变量元数据“变量”
选择。
T = addprop(T,{“OutputFileName”,“OutputFileType”,“如何”},...{“表”,“表”,“变量”});T.Properties
ans = TableProperties with properties: Description: " UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {1x6 cell} variabledescription: {} VariableUnits: {} variableccontinuity: [] RowNames:{}自定义属性(使用t.Properties.CustomProperties.): OutputFileName: [] OutputFileType: [] ToPlot: []
添加自定义属性时使用addprop
,它们的值默认为空数组。您可以使用点语法设置和访问自定义属性的值。
设置输出文件名和类型。这些属性包含表的元数据。对象的逻辑数组如何
财产。此属性包含变量的元数据。在本例中,元素的值如何
属性是真正的
对于每个变量都包含在一个图中,和假
对于每个被排除的变量。
T.Properties.CustomProperties.OutputFileName =“outageResults”;T.Properties.CustomProperties.OutputFileType =“.mat”;T.Properties.CustomProperties.ToPlot = [false false true true true true false];T.Properties
ans =带有属性的TableProperties:描述:" UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {1x6 cell} variabledescription: {} VariableUnits: {} variableccontinuity: [] RowNames:{}自定义属性(使用t.Properties.CustomProperties.): OutputFileName: 'outageResults' OutputFileType: '。mat' ToPlot: [0 0 1 1 10 0]
从T
在一个堆叠的图中使用stackedplot
函数。只绘制损失
,客户
,RestorationTime
值,使用如何
自定义属性作为第二个输入参数。
stackedplot (T, T.Properties.CustomProperties.ToPlot);
当您移动或删除表变量时,预定义和自定义属性都将重新排序,以便它们的值对应于相同的变量。的值在本例中如何
属性的值与标记为绘图的变量保持一致,就像VariableNames
预定义属性保持对齐。
删除客户
变量并显示属性。
T.Customers = [];T.Properties
ans =带有属性的TableProperties:描述:" UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {1x5 cell} variabledescription: {} VariableUnits: {} variableccontinuity: [] RowNames:{}自定义属性(使用t.Properties.CustomProperties.): OutputFileName: 'outageResults' OutputFileType: '。mat' ToPlot: [0 0 1 10 0]
将表转换为时间表,使用停机时间作为行时间。移动地区
到桌子的尽头,还有RestorationTime
在第一个变量之前,使用movevars
函数。注意,属性被适当地重新排序。的RestorationTime
而且损失
变量仍然有在图中包含的指标。
T = table2timeline (T);T = movevars(T,“地区”,“后”,“原因”);T = movevars(T,“RestorationTime”,“之前”1);T.Properties
ans = timeableproperties with properties:描述:" UserData: [] DimensionNames: {'OutageTime' 'Variables'} VariableNames: {'RestorationTime' 'Loss' 'Cause' 'Region'} variabledescription: {} VariableUnits: {} variableccontinuity: [] RowTimes: [1468x1 datetime] StartTime: 2002-02-01 12:18 SampleRate: NaN TimeStep: NaN自定义属性(使用t.Properties.CustomProperties.): OutputFileName: 'outageResults' OutputFileType: '。mat' ToPlot: [1 1 0 0]
属性可以删除表的任何或所有自定义属性rmprop
函数。但是,不能使用它来删除预定义属性T.Properties
的定义,因为这些属性是定义的一部分表格
数据类型。
删除OutputFileName
而且OutputFileType
自定义属性。显示剩余的表属性。
T = rmprop(T,{“OutputFileName”,“OutputFileType”});T.Properties
ans = timeableproperties with properties: Description: " UserData: [] DimensionNames: {'OutageTime' 'Variables'} VariableNames: {'RestorationTime' 'Loss' 'Cause' 'Region'} variabledescription: {} VariableUnits: {} variableccontinucontinuations: [] RowTimes: [1468x1 datetime] StartTime: 2002-02-01 12:18 SampleRate: NaN TimeStep: NaN自定义属性(使用t.Properties.CustomProperties.): ToPlot: [1 1 00]
addprop
|头
|movevars
|readtable
|rmprop
|sortrows
|stackedplot
|表格
|table2timetable