(How) Can I access multiple columns of a table using colon between their (string) lables (variable names)?

10 views (last 30 days)
Say I have the following table T:
T=table;
T.Name={'Alice';'Bob';'Mallory'};
T.Age=randi([18 80],3,1);
T.Salary=randi(100,3,1);
T.Weight=randi([60 90],3,1);
T.Height=randi([150 200],3,1);
I know that I can access a span of columns if I know their index, e.g., T(:,3:5) for sub-table or T{:,3:5} for direct access to the elements therein, and obviously, I can explicitly mention their variable names, as in T(:,{'Salary','Weight','Height'}), but can I use their variable names and colon to specify a span of columns? (something like: T(:,{'Salary' [through] 'Height'}) - I think you could do that in R for instance. But does Matlab have such a feature too? So far, the only workaround that I could think of is the following:
T(:,find(strcmp(T.Properties.VariableNames,'Salary')):find(strcmp(T.Properties.VariableNames,'Height')))
Is there a more efficient way of going about it? Thanks!

Answers (1)

Peter Perkins
Peter Perkins on 26 Oct 2015
Arman, there may be a more compact way to write it using ismember, but that is more or less the right way to do what you want.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!