Copying rows from one table to another

I have a large table of data which I have managed to filter down to the particular entrees I want by deleting unwanted rows. I'd like to be able to create new tables off of this.
For example making a new table consisting off all rows in the original that have the entree X for field y.
I would like to use this with a logical expression such as
rows = table.Name == 'Smith';
vars = {'Gender','Height','Weight'};
newTable = table(rows,vars)
However when do this it informs me that "==" is not defined for cells. What is the correct way to do this?

 采纳的回答

strcmp(table.Name, 'Smith')
would do the trick.

3 个评论

For more generic table indexing, see:
https://uk.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html
For the '0-1' logical indexing see:
https://uk.mathworks.com/help/matlab/math/matrix-indexing.html?searchHighlight=logical%20indexing#bq7egb6-1
So in your example this would work:
newTable = table(rows,:);
This gives you all the vars for the selected rows. Just a small observation: you may want to use a different name for your original table named 'table' to avoid confusion with the function also named 'table'.
Thank you very much for the help!
I saw in the first link you sent that I could use code like the following to return a structure where cells that have entries greater than specific values are listed.
rows = T.Var2>0;
However when I try this I get "Undefined operator '>' for input arguments of type 'cell'". What command would let me do this?
Thank you again for your help, it's much appreciated.
If your table column for a given variable is a cell array, then you need to get the data 'out' of it: http://uk.mathworks.com/help/matlab/cell-arrays.html
So cell2mat would 'Convert cell array to ordinary array of the underlying data type'.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by