MATLAB 列 headers 不是有效的变量名称

MATLAB Column headers not valid variable name

我在 MATLAB 中工作,并尝试将单位添加到列 headers 到 table 值,然后我将插入到 SQLite 数据库中,但我有一个德语字符的列名(例如'ß', 'ä'), 但由于特殊字符,这是无效的。根据我迄今为止发现的所有内容,列 headers 必须是有效的变量名,例如仅限字母数字和“_”。

但是我无法更改我原来的数据库列名所以有人知道 work-around 吗?

我构建 table 并发送到数据库的代码是:

insertData = cell2table(full_matrix,'VariableNames',colnames);
insert(conn,tableName,colnames,insertData);

还有我的一些专栏名称:

'maß','kapazität', 'räder'

非常感谢您的帮助。

像这样创建table first? I would try just passing the cell array of data directly to insert吗:

insert(conn, tableName, colnames, full_matrix);

以上假定它是 cell2table call that is giving you an error related to the special characters. If it's the insert call, then I guess MATLAB won't let you create databases with column names that don't conform to its variable naming conventions. If that's the case, you'll have to convert the column names to something valid, which you can do with either genvarname (for older MATLAB versions) or matlab.lang.makeValidName(建议用于 R2014a 及更新版本):

colNames = {'maß','kapazität', 'räder'};
validNames = genvarname(colNames);
% or...
validNames = matlab.lang.makeValidName(colNames, 'ReplacementStyle', 'hex');

validNames =

  1×3 cell array

    'ma0xDF'    'kapazit0xE4t'    'r0xE4der'

请注意,上述解决方案将无效字符替换为其十六进制等效字符。您还可以更改 'ReplacementStyle' to replace them with underscores or delete them altogether. I would go with the hex values because it gives you the option of converting the column names back to their original string values if you need those for anything later. Here's how you could do that using regexprep, hex2dec, and char:

originalNames = regexprep(validNames, '0x([\dA-F]{2})', '${char(hex2dec())}');

originalNames =

  1×3 cell array

    'maß'    'kapazität'    'räder'