Subset Table using an array of strings

12 ビュー (過去 30 日間)
Metin Akyol
Metin Akyol 2021 年 11 月 12 日
コメント済み: Metin Akyol 2021 年 11 月 12 日
I am using the following code to select a subset of my table based on column names:
table_subset = table(:,ismember(table.Properties.VariableNames, {'col1' 'col2'}));
However, I would like to provide the cell input based on an existing array of column names, like so:
list_of_cols = ['col1' 'col2']
table_subset = table(:,ismember(table.Properties.VariableNames, list_of_cols));
But I am getting an empty table.

回答 (1 件)

Dave B
Dave B 2021 年 11 月 12 日
編集済み: Dave B 2021 年 11 月 12 日
I think you want
list_of_cols = {'col1' 'col2'}
list_of_cols = 1×2 cell array
{'col1'} {'col2'}
or
list_of_cols = ["col1" "col2"]
list_of_cols = 1×2 string array
"col1" "col2"
because list_of_cols as you wrote it evaluates to the wrong thing:
list_of_cols = ['col1' 'col2']
list_of_cols = 'col1col2'
Here's a quick test:
t=table(1,2,3);
list_of_cols = {'Var1' 'Var3'};
t(:,ismember(t.Properties.VariableNames,list_of_cols))
ans = 1×2 table
Var1 Var3 ____ ____ 1 3
  1 件のコメント
Metin Akyol
Metin Akyol 2021 年 11 月 12 日
That makes sense, thank you so much!!

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by