Main Content

本页的翻译已过时。点击此处可查看最新英文版本。

renamevars

重命名表或时间表中的变量

说明

示例

T2 = renamevars(T1,vars,newNames) 使用 newNames 指定的名称重命名 vars 指定的表或时间表变量。

示例

全部折叠

从电子表格中读取表。电子表格为每个表变量提供一个名称。(将包含文本的列读入字符串数组形式的表变量中。)

T = readtable('outages.csv','TextType','string')
T=1468×6 table
      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"   
    "West"         2003-06-18 02:49         0             0    2003-06-18 10:54    "attack"         
    "West"         2004-06-20 14:39    231.29           NaN    2004-06-20 19:16    "equipment fault"
    "West"         2002-06-06 19:28    311.86           NaN    2002-06-07 00:51    "equipment fault"
    "NorthEast"    2003-07-16 16:23    239.93         49434    2003-07-17 01:12    "fire"           
    "MidWest"      2004-09-27 11:09    286.72         66104    2004-09-27 16:37    "equipment fault"
    "SouthEast"    2004-09-05 17:48    73.387         36073    2004-09-05 20:46    "equipment fault"
    "West"         2004-05-21 21:45    159.99           NaN    2004-05-22 04:23    "equipment fault"
    "SouthEast"    2002-09-01 18:22    95.917         36759    2002-09-01 19:12    "severe storm"   
    "SouthEast"    2003-09-27 07:32       NaN    3.5517e+05    2003-10-04 07:02    "severe storm"   
    "West"         2003-11-12 06:12    254.09    9.2429e+05    2003-11-17 02:04    "winter storm"   
    "NorthEast"    2004-09-18 05:54         0             0                 NaT    "equipment fault"
      ⋮

更改变量 LossOutageTimeRestorationTime 的名称。

T = renamevars(T,["Loss","OutageTime","RestorationTime"], ...
                 ["Total Cost","Start of Outage","Restoration"])
T=1468×6 table
      Region       Start of Outage     Total Cost    Customers       Restoration             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"   
    "West"         2003-06-18 02:49           0               0    2003-06-18 10:54    "attack"         
    "West"         2004-06-20 14:39      231.29             NaN    2004-06-20 19:16    "equipment fault"
    "West"         2002-06-06 19:28      311.86             NaN    2002-06-07 00:51    "equipment fault"
    "NorthEast"    2003-07-16 16:23      239.93           49434    2003-07-17 01:12    "fire"           
    "MidWest"      2004-09-27 11:09      286.72           66104    2004-09-27 16:37    "equipment fault"
    "SouthEast"    2004-09-05 17:48      73.387           36073    2004-09-05 20:46    "equipment fault"
    "West"         2004-05-21 21:45      159.99             NaN    2004-05-22 04:23    "equipment fault"
    "SouthEast"    2002-09-01 18:22      95.917           36759    2002-09-01 19:12    "severe storm"   
    "SouthEast"    2003-09-27 07:32         NaN      3.5517e+05    2003-10-04 07:02    "severe storm"   
    "West"         2003-11-12 06:12      254.09      9.2429e+05    2003-11-17 02:04    "winter storm"   
    "NorthEast"    2004-09-18 05:54           0               0                 NaT    "equipment fault"
      ⋮

使用 array2table 函数创建一个包含许多变量的表。为变量添加名称。

T = array2table(rand(5,5))
T=5×5 table
     Var1       Var2       Var3       Var4        Var5  
    _______    _______    _______    _______    ________

    0.81472    0.09754    0.15761    0.14189     0.65574
    0.90579     0.2785    0.97059    0.42176    0.035712
    0.12699    0.54688    0.95717    0.91574     0.84913
    0.91338    0.95751    0.48538    0.79221     0.93399
    0.63236    0.96489    0.80028    0.95949     0.67874

更改变量名称,使每个名称以 "Reading" 开头,以后缀结尾。使用 width 函数确定 T 有多少变量。将表变量指定为一个数值数组。

将数值数组 allVars 转换为字符串数组。通过将每个元素追加到 "Reading" 来创建一个 1×5 字符串数组。

使用 renamevars 函数重命名所有变量。

allVars = 1:width(T);
newNames = append("Reading",string(allVars));
T = renamevars(T,allVars,newNames)
T=5×5 table
    Reading1    Reading2    Reading3    Reading4    Reading5
    ________    ________    ________    ________    ________

    0.81472     0.09754     0.15761     0.14189      0.65574
    0.90579      0.2785     0.97059     0.42176     0.035712
    0.12699     0.54688     0.95717     0.91574      0.84913
    0.91338     0.95751     0.48538     0.79221      0.93399
    0.63236     0.96489     0.80028     0.95949      0.67874

输入参数

全部折叠

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

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

您可以将 vars 指定为下标对象,通过使用 vartype 函数检测指定数据类型的变量。

示例: T = renamevars(T,'Var1','Location') 将表变量 'Var1' 的名称更改为 'Location'

示例: T = renamevars(T,["Var1","Var2"],["Latitude","Longitude"]) 更改两个表变量的名称。

示例: T = renamevars(T,1:width(T),newNames) 重命名所有表变量。width 函数返回的表的宽度等于其变量的数目。

示例: vars = vartype('numeric'); T = renamevars(T,vars,newNames) 重命名 T 中的所有数值变量。

变量的新名称,指定为字符向量、字符串数组或字符向量元胞数组。

newNames 指定的名称的数目必须与 vars 指定的变量的数目相匹配。

提示

  • 您还可以通过设置其 VariableNames 属性来重命名表中的所有变量,如 T.Properties.VariableNames = newNames 中所示。在这种情况下,newNames 必须为字符串数组或字符向量元胞数组。

扩展功能

C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。

在 R2020a 中推出