如何在 MATLAB table 中只重命名几个变量?
How to rename only few variables in MATLAB table?
我想生成一个 table 但只想设置一个变量的变量名,但希望所有其他变量保留其名称。
例如,
说我有这个数据:
User1 = rand(5,1);
User2 = rand(5,1);
User3 = rand(5,2);
我现在可以使用 table:
table(User1 , User2 , User3(:,1))
这给了我这个:
ans =
User1 User2 Var3
________ ________ ________
0.55229 0.049533 0.14651
0.62988 0.48957 0.18907
0.031991 0.19251 0.042652
0.61471 0.12308 0.6352
0.36241 0.20549 0.28187
我想要这个:
ans =
User1 User2 User3
________ ________ ________
0.55229 0.049533 0.14651
0.62988 0.48957 0.18907
0.031991 0.19251 0.042652
0.61471 0.12308 0.6352
0.36241 0.20549 0.28187
我试过这样做:
table(User1 , User2 , User3(:,1), 'VariableNames',{'','','User3'} )
但这给出了错误:
Error using setVarNames (line 33)
The VariableNames property must be a cell array, with each element containing one nonempty
string.
Error in table (line 305)
t = setVarNames(t,vnames); % error if invalid, duplicate, or empty
如何使用 MATLAB 2014b 解决我的问题?
对于我的数据,生成了 d
并循环生成了 table,我想保留 d
的所有值。如果这在某种程度上很重要。
table(User1 , User2 , User3(:,1),'VariableNames', {'User1', 'User2', 'User3'})
根据 MATLAB 的 documentation for the table
data type, you can accomplish this by modifying your table's VariableNames
property.
使用示例table T
:
T = table(rand(3, 1), rand(3, 1), rand(3, 1));
您可以用数字索引变量:
T.Properties.VariableNames{2} = 'Middle_Column'
T.Properties.VariableNames(2:3) = {'Middle_Column', 'End_Column'}
或者您可以使用 table
的隐式字符串比较来对字符串进行索引:
T.Properties.VariableNames{'Var2'} = 'Middle_Column'
T.Properties.VariableNames({'Var2', 'Var3'}) = {'Middle_Column', 'End_Column'}
或者您可以重新分配整个内容:
T.Properties.VariableNames = {'Start_Column', 'Middle_Column', 'End_Column'}
我想生成一个 table 但只想设置一个变量的变量名,但希望所有其他变量保留其名称。
例如, 说我有这个数据:
User1 = rand(5,1);
User2 = rand(5,1);
User3 = rand(5,2);
我现在可以使用 table:
table(User1 , User2 , User3(:,1))
这给了我这个:
ans =
User1 User2 Var3
________ ________ ________
0.55229 0.049533 0.14651
0.62988 0.48957 0.18907
0.031991 0.19251 0.042652
0.61471 0.12308 0.6352
0.36241 0.20549 0.28187
我想要这个:
ans =
User1 User2 User3
________ ________ ________
0.55229 0.049533 0.14651
0.62988 0.48957 0.18907
0.031991 0.19251 0.042652
0.61471 0.12308 0.6352
0.36241 0.20549 0.28187
我试过这样做:
table(User1 , User2 , User3(:,1), 'VariableNames',{'','','User3'} )
但这给出了错误:
Error using setVarNames (line 33)
The VariableNames property must be a cell array, with each element containing one nonempty
string.
Error in table (line 305)
t = setVarNames(t,vnames); % error if invalid, duplicate, or empty
如何使用 MATLAB 2014b 解决我的问题?
对于我的数据,生成了 d
并循环生成了 table,我想保留 d
的所有值。如果这在某种程度上很重要。
table(User1 , User2 , User3(:,1),'VariableNames', {'User1', 'User2', 'User3'})
根据 MATLAB 的 documentation for the table
data type, you can accomplish this by modifying your table's VariableNames
property.
使用示例table T
:
T = table(rand(3, 1), rand(3, 1), rand(3, 1));
您可以用数字索引变量:
T.Properties.VariableNames{2} = 'Middle_Column'
T.Properties.VariableNames(2:3) = {'Middle_Column', 'End_Column'}
或者您可以使用 table
的隐式字符串比较来对字符串进行索引:
T.Properties.VariableNames{'Var2'} = 'Middle_Column'
T.Properties.VariableNames({'Var2', 'Var3'}) = {'Middle_Column', 'End_Column'}
或者您可以重新分配整个内容:
T.Properties.VariableNames = {'Start_Column', 'Middle_Column', 'End_Column'}