将表或时间表变量转换为指定的数据类型

您所在的位置:网站首页 matlab中类型转换 将表或时间表变量转换为指定的数据类型

将表或时间表变量转换为指定的数据类型

2023-12-16 16:07| 来源: 网络整理| 查看: 265

打开实时脚本

检测哪些表变量是日期时间数组。然后使用 datetime 函数作为 convertvars 函数的参数,以指定时区和显示格式。

将停电数据读入一个表中并显示前三行。

T1 = readtable('outages.csv'); head(T1,3) Region OutageTime Loss Customers RestorationTime Cause _____________ ____________________ ______ __________ ____________________ ________________ {'SouthWest'} 01-Feb-2002 12:18:00 458.98 1.8202e+06 07-Feb-2002 16:50:00 {'winter storm'} {'SouthEast'} 23-Jan-2003 00:49:00 530.14 2.1204e+05 NaT {'winter storm'} {'SouthEast'} 07-Feb-2003 21:15:00 289.4 1.4294e+05 17-Feb-2003 08:14:00 {'winter storm'}

T1 中的日期时间数组未设置时区。在未指定表变量的名称或位置的情况下,您可以使用 isdatetime 函数的函数句柄来检测哪些变量是日期时间数组。(函数句柄是一种存储函数关联的变量。您可以使用函数句柄将一个函数传递给另一个函数。例如,指定 @isdatetime 将该句柄传递给 convertvars。)然后,您可以转换所有日期时间变量,以便它们具有时区和不同显示格式。转换多个具有相同数据类型的表变量时,此方法很有用。

调用 convertvars 函数。要修改时区和格式,请指定一个匿名函数,它使用 'TimeZone' 和 'Format' 名称-值对组参数调用 datetime 函数。(匿名函数不会存储在程序文件中。它对于仅需要简短定义的函数非常有用。在本例中,它还允许调用具有多个输入的 datetime,同时按 convertvars 的要求传递只接受一个输入的 convertvars 函数。)显示前三行,以显示格式的变化。

modifyTimeZoneAndFormat = @(x)(datetime(x,'TimeZone','UTC','Format','MMM dd, yyyy, HH:mm z')); T2 = convertvars(T1,@isdatetime,modifyTimeZoneAndFormat); head(T2,3) Region OutageTime Loss Customers RestorationTime Cause _____________ _______________________ ______ __________ _______________________ ________________ {'SouthWest'} Feb 01, 2002, 12:18 UTC 458.98 1.8202e+06 Feb 07, 2002, 16:50 UTC {'winter storm'} {'SouthEast'} Jan 23, 2003, 00:49 UTC 530.14 2.1204e+05 NaT {'winter storm'} {'SouthEast'} Feb 07, 2003, 21:15 UTC 289.4 1.4294e+05 Feb 17, 2003, 08:14 UTC {'winter storm'}


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3