how to extract all columns in between two variable names (column header) of a table?

6 views (last 30 days)
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
T = table(Age,Smoker,Height,Weight)
I wan't to extract the columns of a table by their variable names rather than by matrix indices. For example 'Age' and 'weight' coulmns of table T can be extracted as
T(:,{'Age', 'Weight'})
But how do I extract all the columns from 'Age' to 'Weight' ? The following syntax doesn't work
T(:,{'Age':'Weight'})
Please suggest if you have a solution. Thank you.

Accepted Answer

Stephen23
Stephen23 on 29 Mar 2021
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
T = table(Age,Smoker,Height,Weight)
T = 5×4 table
Age Smoker Height Weight ___ ______ ______ ______ 38 true 71 176 43 false 69 163 38 true 64 131 40 false 67 133 49 true 64 119
L = 'Age';
R = 'Weight';
[X,Y] = ismember({L,R},T.Properties.VariableNames);
T(:,Y(1):Y(2))
ans = 5×4 table
Age Smoker Height Weight ___ ______ ______ ______ 38 true 71 176 43 false 69 163 38 true 64 131 40 false 67 133 49 true 64 119

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!