Extract all columns from a table where the column names "start with" a string
266 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
newbie9
le 28 Août 2019
Commenté : Walter Roberson
le 27 Mai 2022
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?
0 commentaires
Réponse acceptée
Walter Roberson
le 28 Août 2019
mask = startsWith( mytable.Properties.VariableNames, 'F');
Fcols = mytable(:,mask);
Plus de réponses (1)
Arnaud Delorme
le 27 Mai 2022
Modifié(e) : Arnaud Delorme
le 27 Mai 2022
colNames = fieldnames(mytable);
[~,inds] = intersect(colNames, { 'A1' 'A2' } );
mytable = mytable(:,sort(inds));
1 commentaire
Walter Roberson
le 27 Mai 2022
This would not get you anything that mytable(:, { 'A1' 'A2' } ) would not give, other than preserving relative order of variables.
Voir également
Catégories
En savoir plus sur Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!