equivalent of isfield for tables
137 ビュー (過去 30 日間)
古いコメントを表示
Jonathan C. Lansey
2014 年 6 月 4 日
回答済み: Georges-Andre Chaudron
2017 年 1 月 19 日
I'd like to know if my Matlab table has a particular column. If it was a structure I'd use isfield. Is there a built in function for this?
0 件のコメント
採用された回答
Georges-Andre Chaudron
2017 年 1 月 19 日
I would also like to suggest using ismember()
For instance:
f1 = 1; f2 = 10; t = table(f1,f2)
t =
f1 f2
__ __
1 10
ismember('f1', t.Properties.VariableNames)
logical
1
It also works for a range of values:
ismember({'f0','f1','f2'}, t.Properties.VariableNames)
1×3 logical array
0 1 1
0 件のコメント
その他の回答 (1 件)
Roger Parkyn
2015 年 9 月 15 日
編集済み: Roger Parkyn
2015 年 9 月 15 日
This issue was also raised by Pawel Kusmierek ( 123242-why-isfield-does-not-work-with-tables-but-fieldnames-does ). He gave a reasonably tidy work-around, in short:
name_exists = any(strcmp('Var_Name',fieldnames(Table_Name)))
1 件のコメント
Kelly Kearney
2015 年 9 月 15 日
Note that fieldnames returns all object properties, which for a table includes both variable names and the Properties structure. To get just the column/variable names, query the VariableNames property:
t = table({'one';'two'}, [1;2])
t =
Var1 Var2
_____ ____
'one' 1
'two' 2
>> t.Properties.VariableNames
ans =
'Var1' 'Var2'
>> fieldnames(t)
ans =
'Var1'
'Var2'
'Properties'
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!