Matlab-在表格标题中使用特殊字符 [英] Matlab - Using special characters in table header
问题描述
我已经用一个结构中的数据构造了一个表,现在希望在导出表之前将自定义标头添加到列中.我发现以下命令:
I have constructed a table with data from a struct and now wish to add custom headers to the columns before exporting the table. I found the following command:
T.Properties.VariableNames{'OldHeader'} = 'NewHeader';
但是,此命令不允许我在标题中使用空格或特殊字符.我的表包含来自处理后的实验室数据的输出,我希望具有诸如"Vol.[mL]"和"Conc.[wt%]"之类的标题.
This command however does not allow me to use spaces or special characters for my headers. My table contains the output from processed lab data and I wish to have headers like "Vol. [mL]" and "Conc. [wt%]".
使用 matlab文档中的示例进行说明:
To illustrate using the example from matlab documentation:
S.Name = {'CLARK';'BROWN';'MARTIN'};
S.Gender = {'M';'F';'M'};
S.SystolicBP = [124;122;130];
S.DiastolicBP = [93;80;92];
T = struct2table(S)
T.Properties.VariableNames{'Gender'} = 'Sex';
以上方法有效,但我只能使用普通字符且不能使用空格.我的问题是如何将性别"更改为体积[mL]"-甚至可能吗?
The above works, but restricts me to normal characters and no spaces. My question is how to change "Gender" to "Vol. [mL]" - if even possible?
推荐答案
正如@Jubobs在注释中已经提到的那样,存在一些用于命名变量的规则,这些规则会阻止您选择所需的确切名称.从文档中,我通过对该主题进行了搜索:>
As @Jubobs already mentioned in the comment, there are some rules for naming variables that prevent you from choosing the exact name that you want. From the documentation that I found by googling the topic:
有效的变量名以字母开头,后跟字母,数字或下划线.MATLAB®区分大小写,因此A和a不区分大小写相同的变量.变量名称的最大长度是值namelengthmax命令返回.
A valid variable name starts with a letter, followed by letters, digits, or underscores. MATLAB® is case sensitive, so A and a are not the same variable. The maximum length of a variable name is the value that the namelengthmax command returns.
您不能使用与MATLAB关键字相同的名称定义变量,例如是否或结束.有关完整列表,请运行iskeyword命令.
You cannot define variables with the same names as MATLAB keywords, such as if or end. For a complete list, run the iskeyword command.
但是,我可以想到两种简单的方法来解决此问题:
However, I could think of two easy ways to work around this:
- 不同的名称,例如,您可以使用变量名称
Vol_ml
- 将名称存储在列表中,第一个变量名称可能带有诸如
v1
之类的索引代码,然后可以将v1
用作第一个变量的名称./li>
- Different names, for example you can use the variable name
Vol_ml
- Store the names in a list, perhaps with an index code like
v1
for the first variable name, then you can makev1
the name of the first variable.
这篇关于Matlab-在表格标题中使用特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!