Basic Question: How to index into table by a text column

Hello, new to Matlab. I have a table imported into Matlab. How to I extract all data in all columns from that table in only the rows labelled "hardness"? I do know that I can't extract from a table (at least I think I can't). I was able to convert the table to a cell array but I am still unable to figure out how to get just the rows with the text label "hardness" out of this table.

3 个评论

A table does not have labels. You possibly have one of the columns containing strings that can be interpreted as labels, but you haven't told us the name of that column.
Have you looked at the examples in the table documentation?
Note that there is absolutely no need to convert a table to a cell array. You can do filtering directly on tables.
Not by the name labels, no, but it does have the .RowNames property which I'd presume qualifies in concept to OP???? At least, that'd be my guess...
Except that you can't have two rows with the same name, so you wouldn't be able to select only the rows labelled "hardness" since there'll be at most one.

请先登录,再进行评论。

 采纳的回答

Presuming you used the names when creating the table, index by the desired name values--

4 个评论

when i try this i get "Unrecognized row name 'hardness', although 1/3 of the values have this for a field. Does it matter that "hardness" is not the text in the first column but the 4th column? I am trying to extract all columns of data for rows that have the text 'hardness' in the 4th column. its possible I am missing something very obvious, I just started using Matlab last week.
You cannot use the row names for labelling since each row name has to be unique.
What you want is exactly what I've written in my answer: filter the table based on the string content of a column.
Yes, this worked once I was able to read instructions accurately! thank you very much :)
Since it was actually Guillaume's Answer that works (I didn't read the doc's well enough initially to catch that the .name property must be unique and don't have a recent release that has the class to test), it would be "more better" for you to ACCEPT his answer than mine that's incorrect...after which I'll delete it so as to not leave confusing info hanging around...

请先登录,再进行评论。

更多回答(1 个)

To return all rows of a table whose column somecolumnname matches a specific string:
filteredtable = yourtablename(strcmp(yourtablename.somecolumnname, stringtomatch), :);
e.g.
t = table({'hardness', 'aaaa', 'hardness', 'bb'}', [1;2;3;4], [pi; exp(1); log(2); 0], 'VariableNames', {'label', 'value', 'someothername'})
filteredt = t(strcmp(t.label, 'hardness'), :)
edit: since we now know that the label column is the 4th, you can do this:
filteredtable = yourtable(strcmp(yourtable{:, 4}, 'hardness'), :) %keep only those rows whose 4th column is 'hardness'

2 个评论

This sounds like a good candidate for using a categorical variable rather than text. That would allow a syntax such as
filteredtable = yourTable(yourTable.yourVar == 'hardness', :)
which you might find more readable. Note also using dot subscripting (yourTable.yourVar) rather than braces. For one variable, that's a simpler way to go.
Yeah, until I reread the doc's that's how I thought the .name property would work...

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by