主要内容

convertvars

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

说明

T2 = convertvars(T1,vars,dataType) 将指定的变量转换为指定的数据类型。输入参量 T1 可以是表或时间表。

虽然您可以将 dataType 指定为数据类型的名称,但也可以将其指定为函数句柄。如果指定为函数句柄,则它表示用于转换或修改 vars 指定的变量的函数句柄。同样,vars 可以包含 T1 中变量的变量名称或位置,也可以是标识这些变量的函数的句柄。

示例

示例

全部折叠

从包含停电数据的电子表格中读取一个表。该表包含显示每次停电的区域和原因的文本变量,显示停电和恢复时间的日期时间变量,以及显示电力损耗和受影响客户数的数值变量。显示前五行。

T1 = readtable('outages.csv');
head(T1,5)
       Region           OutageTime        Loss     Customers     RestorationTime            Cause       
    _____________    ________________    ______    __________    ________________    ___________________

    {'SouthWest'}    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    {'winter storm'   }
    {'SouthEast'}    2003-01-23 00:49    530.14    2.1204e+05                 NaT    {'winter storm'   }
    {'SouthEast'}    2003-02-07 21:15     289.4    1.4294e+05    2003-02-17 08:14    {'winter storm'   }
    {'West'     }    2004-04-06 05:44    434.81    3.4037e+05    2004-04-06 06:10    {'equipment fault'}
    {'MidWest'  }    2002-03-16 06:18    186.44    2.1275e+05    2002-03-18 23:23    {'severe storm'   }

将变量 RegionCause 转换为分类变量。请注意,显示分类值时不带引号。

T2 = convertvars(T1,{'Region','Cause'},'categorical');
head(T2,5)
     Region         OutageTime        Loss     Customers     RestorationTime          Cause     
    _________    ________________    ______    __________    ________________    _______________

    SouthWest    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    winter storm   
    SouthEast    2003-01-23 00:49    530.14    2.1204e+05                 NaT    winter storm   
    SouthEast    2003-02-07 21:15     289.4    1.4294e+05    2003-02-17 08:14    winter storm   
    West         2004-04-06 05:44    434.81    3.4037e+05    2004-04-06 06:10    equipment fault
    MidWest      2002-03-16 06:18    186.44    2.1275e+05    2002-03-18 23:23    severe storm   

可以很方便地将变量转换为提供不同功能的数据类型。例如,现在 T2.Region 是一个分类变量,您可以使用 pie 函数按区域制作停电数据的饼图。但是,您不能使用 T1.Region 作为 pie 的输入参量,因为该变量包含文本,而不是分类数据。

pie(T2.Region)

Figure contains an axes object. The hidden axes object contains 10 objects of type patch, text. These objects represent MidWest, NorthEast, SouthEast, SouthWest, West.

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

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

T1 = readtable('outages.csv');
head(T1,3)
       Region           OutageTime        Loss     Customers     RestorationTime          Cause      
    _____________    ________________    ______    __________    ________________    ________________

    {'SouthWest'}    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    {'winter storm'}
    {'SouthEast'}    2003-01-23 00:49    530.14    2.1204e+05                 NaT    {'winter storm'}
    {'SouthEast'}    2003-02-07 21:15     289.4    1.4294e+05    2003-02-17 08:14    {'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'}

输入参数

全部折叠

输入表,指定为表或时间表。

如果 T1 是时间表,则不能使用 convertvars 转换其行时间,因为行时间不包含在时间表变量中。行时间是时间表元数据。

输入表或时间表中的变量,指定为字符串数组、字符向量、字符向量元胞数组、pattern 标量、数值数组、逻辑数组或函数句柄。

如果 vars 是函数句柄,则该函数必须接受一个输入参量、标识其数据类型,并返回逻辑标量。例如,使用 isnumeric 函数来检测哪些变量是数值。

示例: T2 = convertvars(T1,'Region','categorical') 转换变量 Region 的类型。

示例: T2 = convertvars(T1,[1,3:6],'string') 将位置指定的变量转换为字符串数组。

示例: T2 = convertvars(T1,@isnumeric,'int32') 将所有数值变量转换为 32 位整数。

转换后的变量的数据类型,指定为字符向量、字符串标量或函数句柄。

如果 dataType 是函数句柄,则该函数必须接受一个输入参量并将其转换为另一种数据类型。例如,string 函数将输入参量转换为字符串数组。

下表显示了许多常用数据类型的名称。

'single'单精度数
'double'双精度数
'int8'有符号 8 位整数
'int16'有符号 16 位整数
'int32'有符号 32 位整数
'int64'有符号 64 位整数
'uint8'无符号 8 位整数
'uint16'无符号 16 位整数
'uint32'无符号 32 位整数
'uint64'无符号 64 位整数
'logical'逻辑值 1 (true) 或 0 (false)
'string'字符串数组
'cell'元胞数组
'cellstr'字符向量元胞数组
'categorical'分类数组
'datetime'日期时间数组
'duration'持续时间数组
'calendarDuration'日历持续时间数组

如果将 'char' 指定为数据类型,则 convertvars 会将变量转换为字符数组。最佳做法是避免创建字符数组形式的表或时间表变量。而是考虑将变量转换为字符串数组、分类数组或字符向量元胞数组。

示例: T2 = convertvars(T1,'OutageTime','datetime') 转换变量 OutageTime 的类型。

示例: T2 = convertvars(T1,'Region',@categorical) 使用 categorical 函数的函数句柄转换变量。

替代功能

您还可以使用 VariableTypes 属性来转换表和时间表变量。与 convertvars 不同,该属性使您能够将不同变量转换为不同数据类型。 (自 R2024b 起)

例如,为表 T 的变量赋予新数据类型,以便第一个变量是分类数组,最后一个变量是字符串数组。

T = readtable('outages.csv');
head(T,3)
       Region           OutageTime        Loss     Customers     RestorationTime            Cause       
    _____________    ________________    ______    __________    ________________    ___________________

    {'SouthWest'}    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    {'winter storm'   }
    {'SouthEast'}    2003-01-23 00:49    530.14    2.1204e+05                 NaT    {'winter storm'   }
    {'SouthEast'}    2003-02-07 21:15     289.4    1.4294e+05    2003-02-17 08:14    {'winter storm'   }
T.Properties.VariableTypes = ["categorical" "datetime" "double" "double" "datetime" "string"];
head(T,3)
     Region         OutageTime        Loss     Customers     RestorationTime         Cause     
    _________    ________________    ______    __________    ________________    ______________

    SouthWest    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    "winter storm"
    SouthEast    2003-01-23 00:49    530.14    2.1204e+05                 NaT    "winter storm"
    SouthEast    2003-02-07 21:15     289.4    1.4294e+05    2003-02-17 08:14    "winter storm"

扩展功能

全部展开

基于线程的环境
使用 MATLAB® backgroundPool 在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool 加快代码运行速度。

版本历史记录

在 R2018b 中推出