MATLAB: How to select multiple columns in a table using variable names and wildcards

tables indexing wildcard

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!

Best Answer

I am not using tables, so you should probably wait for a better answer, but here is one way that I could see. Say we have the table:
T =
5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Smith 38 71 176 124 93
Johnson 43 69 163 109 77
Williams 38 64 131 125 83
Jones 40 67 133 117 75
Brown 49 64 119 122 80
and we want all variables that contain the string 'ei' (pattern):
>> hasMatch = ~cellfun('isempty', regexp(T.Properties.VariableNames, 'ei', 'once')) ;
>> T(:, T.Properties.VariableNames(hasMatch))
ans =
5×2 table
Height Weight
______ ______
Smith 71 176
Johnson 69 163
Williams 64 131
Jones 67 133
Brown 64 119
Here we are using regular expressions, so you must use regexp types of "wildcards".