Extract all columns from a table where the column names "start with" a string

171 次查看(过去 30 天)
How can I extract all columns from a table where the column names "start with" a string?
For example, in
mytable = array2table(NaN(4,5));
mytable.Properties.VariableNames = {'A1', 'A2', 'B1', 'F2', 'F4'};
How would I extract all the "F*" columns in to a new table?

采纳的回答

Walter Roberson
Walter Roberson 2019-8-28
mask = startsWith( mytable.Properties.VariableNames, 'F');
Fcols = mytable(:,mask);

更多回答(1 个)

Arnaud Delorme
Arnaud Delorme 2022-5-27
编辑:Arnaud Delorme 2022-5-27
colNames = fieldnames(mytable);
[~,inds] = intersect(colNames, { 'A1' 'A2' } );
mytable = mytable(:,sort(inds));
  1 个评论
Walter Roberson
Walter Roberson 2022-5-27
This would not get you anything that mytable(:, { 'A1' 'A2' } ) would not give, other than preserving relative order of variables.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by