How can I select multiple columns in a table using variable names and wildcards?
Afficher commentaires plus anciens
Hi all,
I am trying to index and extract a series of variables from a table i've created based on the VariableNames. I would rather index using the VariableNames as opposed to numerical or logical index due to the fact that there might be different numbers of trials for the variables.
Using the below code I can call one variable at a time. The '_1' represents the trial number which could go up to '_20'.
C1vC2Trials = C1MPVNormVariables(:,'C1MPVNorm_1');
However when trying to use the * wildcard to call all trials for that variable I get an error.
Error: Unrecognized variable name 'C1MPVNorm_*'.
Is this possible to do with tables or is there a easy work around to find the number of VariableNames in the table which correspond to the ones I wish to extract?
Thanks in advance!
Réponse acceptée
Plus de réponses (2)
Roman Romanovski
le 6 Mai 2019
6 votes
T(:,ismember(T.Properties.VariableNames, {""list of variables""}))
This will get you there for the variable names part. Above already answered wild card.
> T(:, contains(T.Properties.VariableNames, 'C1MPVNorm_'))
5 commentaires
Roman Romanovski
le 6 Mai 2019
Modifié(e) : Roman Romanovski
le 6 Mai 2019
Although. I wish there was syntax like
T.{'''list of variables'''}
instead of
T(:,ismember(T.Properties.VariableNames, {""list of variables""}))
Peter Perkins
le 9 Mai 2019
I may be misunderatanding what {""list of variables""} is intended to prepresent, but if it works as the 2nd input to ismember, it will work as a table variables subscript. So T(:,ListOfVars) or T{:,ListOfVars}, depending on whether you want a subtable or the contents.
T.{'''list of variables'''} doesn't make sense, because dot subscripting extracts exactly one variable. Dot, one variable. Braces, any number, including none or one.
Roman Romanovski
le 9 Mai 2019
Modifié(e) : Roman Romanovski
le 9 Mai 2019
Both would work.
My method protects from an error if the variable in the list doesn't exist in the table.
I'm not sold on your dot = one explanation. What if T is an array of Table? T.x returns array of x. Besides that misses my point. What I am trying to say is it would be nice to be able to select multiple columns by name without requiring a row index.
Peter Perkins
le 14 Mai 2019
Tables have variables, not columns. "Variables", because the things in a table can themselves have multiple columns. So yes, dot subscripting extracts exactly one variable.
The syntax to extract multiple variables is T{:,ListOfVars}.
Zzz
le 6 Mai 2021
The correct syntax is T(:, {'col_name_1', 'col_name_2'}) to get the data like a table.
Chenye Shen
le 22 Oct 2021
1 vote
You can refer to this。
2 commentaires
To expand on the link that Chenye has provided: new features are added all the time. In particular, tables have supported patters for variable names since ... I actualy don't recall exactly when. The doc in that link show an example, but for the record:
t = table([1;2;3],[4;5;6],[7;8;9],VariableNames=["x1" "x2" "y"])
t(:,"x"+digitsPattern)
Also, of course, use "strings", not 'cell arrays of char vectors'!
Steven Lord
le 21 Sep 2023
The ability to use pattern objects to index into table rows, variables, and properties was introduced in release R2022a.
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!