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.
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

dpb
dpb 2016년 10월 10일

0 개 추천

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개)

Guillaume
Guillaume 2016년 10월 10일
편집: Guillaume 2016년 10월 10일
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에 대해 자세히 알아보기

질문:

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