(How) Can I access multiple columns of a table using colon between their (string) lables (variable names)?
Mostrar comentarios más antiguos
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 comentario
Walter Roberson
el 25 de Oct. de 2015
My thought was that that conceptually when you use the names, the columns are unordered. I could be wrong about that.
Respuestas (1)
Peter Perkins
el 26 de Oct. de 2015
0 votos
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.
Categorías
Más información sobre Tables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!