フィルターのクリア

Changing the name of the variables in table

8 ビュー (過去 30 日間)
Shelender Kumar
Shelender Kumar 2018 年 11 月 2 日
コメント済み: Peter Perkins 2018 年 11 月 6 日
Let us say I have an output file (called combined) which is an array so To write to a file, first I convert it into the table but now I do not know how to change the name of the variables
T = array2table(combined);
Now the variables names are combined1, combined2 and so on but I want to give it my own names like 150K, 140K etc. Could you help me with this
  1 件のコメント
Shelender Kumar
Shelender Kumar 2018 年 11 月 3 日
I did use
a=table(1,2,3,'VariableNames',{'k146','k147','k148'}
and it worked

サインインしてコメントする。

採用された回答

Caglar
Caglar 2018 年 11 月 2 日
編集済み: Caglar 2018 年 11 月 2 日
You can do that on initialization,
a=table(1,2,3,'VariableNames',{'One','Two','Final'})
or after creation,
a.Properties.VariableNames={'Five','Six','Seven'}
Note that, sadly, Matlab does not let you define variable names before you put at least one row of data.
b=table('VariableNames',{'Test'})
Error using table (line 307)
The VariableNames property must contain one name for each variable in the table.
  4 件のコメント
Caglar
Caglar 2018 年 11 月 2 日
You can use it like _146k or k146. If you really need to keep the original variable name, you can save them in descriptions of the table.
a.Properties.VariableDescriptions={'146k','145k'}
They wont show up on variable but you can load them back whenever you need again.
Peter Perkins
Peter Perkins 2018 年 11 月 6 日
Shelender, array2table allows you to provide var names if you don't want the defaults. See the doc.
Caglar, the reason why
table('VariableNames',{'Test'})
errors is because you are providing a name but no variable. You can't name something that isn't there, and there's a difference between "has no rows" and "isn't there at all". If you want a table with no rows, one variable, and a specific name, do something like this
table(zeros(0,1),'VariableNames',{'Test'})
or in recent versions (R2018a and later, IIRC), this
table('Size',[0,1],'VariableTypes',{'double'}, ,'VariableNames',{'Test'})

サインインしてコメントする。

その他の回答 (1 件)

awezmm
awezmm 2018 年 11 月 2 日
編集済み: awezmm 2018 年 11 月 2 日
I think you can do something like: T.Properties.VariableNames = {'newname1' 'newname2'}
  2 件のコメント
Shelender Kumar
Shelender Kumar 2018 年 11 月 2 日
Thanks but I am getting an error '146k' is not a valid variable name.
awezmm
awezmm 2018 年 11 月 2 日
The issue is that you cant start variable names with numbers. You can have to some sort of string at the beginning. Maybe you can do 'Resolution_146k' or give some other title so it describes it well

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by