Select columns of a table using matching column names
251 ビュー (過去 30 日間)
古いコメントを表示
Hello everyone!
I'm facing the following problem: I have a cell array containing strings, let's call it "v"
On the other hand I have a huge table , let's call it "T", from which I want to select only the columns that have the column name that corresponds to the names contained in vector "v".
Any kind of help will be appreciated
Thank you
0 件のコメント
採用された回答
Voss
2022 年 6 月 14 日
v = {'Var1' 'Var3'}
T = table([1;2;3],[4;5;6],[7;8;9])
% a table of the selected columns of T:
new_T = T(:,v)
% or, the data from the selected columns of T:
new_T_data = T{:,v}
2 件のコメント
Voss
2022 年 6 月 15 日
If not all elements of v are column names of T, you can use ismember to get just the elements of v that do correspond to column names of T:
v = {'Var1' 'Var3' 'Var4'}
T = table([1;2;3],[4;5;6],[7;8;9])
v_to_use = v(ismember(v,T.Properties.VariableNames))
And use that the same as v was used before:
new_T = T(:,v_to_use)
new_T_data = T{:,v_to_use}
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!