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 件のコメント

Guillaume
Guillaume 2016 年 10 月 10 日
編集済み: Guillaume 2016 年 10 月 10 日
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.
dpb
dpb 2016 年 10 月 10 日
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...
Guillaume
Guillaume 2016 年 10 月 10 日
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.

サインインしてコメントする。

 採用された回答

dpb
dpb 2016 年 10 月 10 日

0 投票

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

4 件のコメント

Elizabeth Grieco
Elizabeth Grieco 2016 年 10 月 10 日
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.
Guillaume
Guillaume 2016 年 10 月 10 日
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.
Elizabeth Grieco
Elizabeth Grieco 2016 年 10 月 10 日
Yes, this worked once I was able to read instructions accurately! thank you very much :)
dpb
dpb 2016 年 10 月 11 日
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 件)

Guillaume
Guillaume 2016 年 10 月 10 日
編集済み: Guillaume 2016 年 10 月 10 日

0 投票

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 件のコメント

Peter Perkins
Peter Perkins 2016 年 10 月 13 日
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.
dpb
dpb 2016 年 10 月 13 日
Yeah, until I reread the doc's that's how I thought the .name property would work...

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

質問済み:

2016 年 10 月 10 日

コメント済み:

dpb
2016 年 10 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by