Sort table by two columns using column name
21 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Shana Hartel
le 20 Déc 2018
Commenté : Peter Perkins
le 21 Déc 2018
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 commentaires
Peter Perkins
le 21 Déc 2018
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"
Réponse acceptée
Cris LaPierre
le 20 Déc 2018
sortrows(tblA,{'Height','Weight'})
Use square brackets if you want to use indexing to specify the columns
sortrows(tblA,[1 4])
Plus de réponses (1)
madhan ravi
le 20 Déc 2018
Another possible solution:
sortrows(yourtable,[yourtable{:,1} yourtable{:,2}])
0 commentaires
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!