【发布时间】:2016-03-14 16:12:23
【问题描述】:
我希望对以下数据使用makeValidName 函数:
Id Val Random Desc
a 1.1 0.036835624 Bread Cheese
b 2.2 0.020442492 Fish Bread
c -3.3 0.020050676 Cheese Fish
d #N/A 0.017619332 Bread Cheese
e -5.4 0.014973153 Fish Bread
f 6.6 0.014648887 Cheese Fish
g -7.6 0.014071844 Bread Cheese
h 8 0.014013118 Fish Bread
但是,当我导入表格(使用 readtable 从 xlsx 读取)时,它看起来像这样:
输入数据 =
Id Val Random Desc
____ ____________________ ________ ______________
'a ' '1.1' 0.036836 'Bread Cheese'
'b' '2.2' 0.020442 'Fish Bread'
'c' '-3.3' 0.020051 'Cheese Fish'
'd' 'ActiveX VT_ERROR: ' 0.017619 'Bread Cheese'
'e' '-5.4' 0.014973 'Fish Bread'
'f' '6.6' 0.014649 'Cheese Fish'
'g' '-7.6' 0.014072 'Bread Cheese'
'h' '8' 0.014013 'Fish Bread'
如何防止它将Val 中的条目从数字转换为字符串?这使得无法使用makeValidName。我需要在所有行和列中应用makeValidName,因为表非常大,单独命名适当的列是不可行的。那么实现这一目标的最优雅的方式是什么?
当前代码:
varnames = inputData.Properties.VariableNames;
for ii = 1:length(varnames)
inputData.(varnames{ii})= matlab.lang.makeValidName(inputData.(varnames{ii}));
end
产生错误:
使用 matlab.lang.makeValidName 时出错(第 72 行)第一个输入必须是 字符串或字符串向量元胞数组。
并在Val等列中产生不良结果:
输入数据 =
Id Val Random Desc
___ __________________ ________ _____________
'a' 'x1_1' 0.036836 'BreadCheese'
'b' 'x2_2' 0.020442 'FishBread'
'c' 'x_3_3' 0.020051 'CheeseFish'
'd' 'ActiveXVT_ERROR_' 0.017619 'BreadCheese'
'e' 'x_5_4' 0.014973 'FishBread'
'f' 'x6_6' 0.014649 'CheeseFish'
'g' 'x_7_6' 0.014072 'BreadCheese'
'h' 'x8' 0.014013 'FishBread'
【问题讨论】:
-
您确定您了解
makeValidName的用途吗?Val列显示的行为与函数的行为一致。 -
是的。我希望它能够做到这一点,只是我需要它只为非数字条目做到这一点。由于#NA readtable 已将整个 Val 列转换为字符串,即使除一个之外的所有条目都是数字。这导致数字在
makeValideName下被进一步转换。所以理想情况下 readtable 需要正确区分数字或字符串列,或者我需要在导入后转换/清理数据
标签: matlab data-cleaning matlab-table