Sort table by two columns using column name

조회 수: 26 (최근 30일)
Shana Hartel
Shana Hartel 2018년 12월 20일
댓글: Peter Perkins 2018년 12월 21일
Hello!
I can sort a table with: sortrows(tableName, 'columnName1').
I can also sort the same table by 2 columns using: sortrows(tableName,[columnNumber1 columnNumber2])
How can I sort the table by 2 columns using column name?
The following does not work: sortrows(tableName, ['columnName1' 'columnName2']). I get an error saying "unrecognized variable name 'columnName1ColumnName2'.
Thanks in advance!
  댓글 수: 2
madhan ravi
madhan ravi 2018년 12월 20일
upload your table
Peter Perkins
Peter Perkins 2018년 12월 21일
The issue here is that ['columnName1' 'columnName2'] concatenates those two char row vectors to make one long one. As Chris says, you need either a cell array of char row vectors
>> {'columnName1' 'columnName2'}
ans =
1×2 cell array
{'columnName1'} {'columnName2'}
or in recent versions of MATLAB, a string array
>> ["columnName1" "columnName2"]
ans =
1×2 string array
"columnName1" "columnName2"

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

채택된 답변

Cris LaPierre
Cris LaPierre 2018년 12월 20일
Use curly braces. From the doc page for sortrows
sortrows(tblA,{'Height','Weight'})
Use square brackets if you want to use indexing to specify the columns
sortrows(tblA,[1 4])

추가 답변 (1개)

madhan ravi
madhan ravi 2018년 12월 20일
Another possible solution:
sortrows(yourtable,[yourtable{:,1} yourtable{:,2}])

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by