2

我正在运行这样的东西:

table = readtable(path,'ReadVariableNames',true);

在 .csv 文件中,第一行的所有单元格都包含字符串标识符,例如'99BM''105CL'等。所有内容都以数字开头。上面的命令给出了一个带有变量名的表,如'x99BM','x105CL'等。

有可能摆脱这个'x'吗?我需要将这些标识符与另一个表中明确的列进行比较'x'

4

2 回答 2

1

不,表变量名称不能以数字开头,因为它们遵循与普通变量相同的命名约定。会'x'自动添加readtable以创建有效名称。您应该在调用时注意到此警告readtable

Warning: Variable names were modified to make them valid MATLAB identifiers.
The original names are saved in the VariableDescriptions property.

所以,你不能摆脱表'x' 。但是,如果您需要进行任何比较,您可以针对保存在VariableDescriptions属性中的原始值进行比较,其格式如下:

>> T.Properties.VariableDescriptions

ans =

  1×2 cell array

    'Original column heading: '99BM''    'Original column heading: '105CL''

您可以使用正则表达式解析这些,例如:

originalNames = regexp(T.Properties.VariableDescriptions, '''(.+)''', 'tokens', 'once');
originalNames = vertcat(originalNames{:});

originalNames =

  2×1 cell array

    '99BM'
    '105CL'

然后在您需要做的任何字符串比较中使用它们。

于 2017-09-14T16:11:41.353 回答
0

不会。正如 gnovice 所说,readtable 函数会自动重命名无效名称。它通过调用matlab.lang.makevalidname并将输出设置为列名来实现这一点。

如果我理解正确,您正在将一个表中的列的内容与另一个表的列的名称进行比较。在这种情况下,contains(['x' <contents of single row of column>], table.VariableNames)会将 x 附加到表列的一行中的值(对于此实现,您需要遍历表的每一行),然后将此字符串与表的变量名称进行比较。您也可以使用 arrayfun 或其他东西在一行中执行此操作,但我现在正在从内存中执行此操作并且无法回忆正确的语法。

于 2017-09-14T17:04:56.397 回答