Sorting a cell array rows according to string column alphabetically
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi everyone, a matlab beginner question :
I have a cell array like the following:

I want to sort the array rows alphabetically according to the first column. (and I want the first row to stay at the top as it is the header row)
I have tried to use sort and sortrows but I am getting the error : Error using sort Input argument must be a cell array of character vectors.
How can I sort this cell array?
Thanks
0 Kommentare
Antworten (1)
Jos (10584)
am 8 Nov. 2017
Assuming your cell array is namens C:
C = {'c1' 'c2','c3' ; 'c' 1 2 ; 'a' 2 3 ; 'b' 3 4}
[~, ix] = sort(C(2:end,1)) % sort the first column from 2nd row onwards and get the indices
C(2:end,:) = C(ix+1,:) % do not forget to add a 1 :)
4 Kommentare
Jos (10584)
am 9 Nov. 2017
the content of the cells are not character arrays, but a cell containing a character array! See the differences between:
C1(1) = {'aaa'}
C2{1} = {'bbb'}
C3{1} = 'ccc'
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!