(How) Can I access multiple columns of a table using colon between their (string) lables (variable names)?
10 views (last 30 days)
Show older comments
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!
1 Comment
Walter Roberson
on 25 Oct 2015
My thought was that that conceptually when you use the names, the columns are unordered. I could be wrong about that.
Answers (1)
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.
0 Comments
See Also
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!