Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sorting a Cell Array

1 view
Skip to first unread message

Chmical

unread,
Mar 11, 2010, 4:25:04 PM3/11/10
to
I have a cell array with a header row and column made up of both numbers and letters. I would like to do a normal sort (like you would do with Excel). Is there a way to accomplish this? I have tried the normal (in the help) sort functions with no luck.

Example array

test = {'PC','S','PO';84938,'O','Q34ef3';84548,'P','Q34g53';84248,'O','Q37h93';};

test =

'PC' 'S' 'PO'
[84938] 'O' 'Q34ef3'
[84548] 'P' 'Q34g53'
[84248] 'O' 'Q37h93'

Thank you in advance
Chmical

Oleg Komarov

unread,
Mar 11, 2010, 4:40:23 PM3/11/10
to
"Chmical"

sort by first col (trsnforming num2str).
sortrows(cellfun(@num2str, test(2:end,:),'un',0),1)

If you have the statistics tb use the dataset object.
Oleg

Chmical

unread,
Mar 12, 2010, 10:06:05 AM3/12/10
to
"Oleg Komarov" <oleg.komaro...@hotmail.it> wrote in message <hnbo07$adj$1...@fred.mathworks.com>...

Thank you Oleg works great. One more question... If I sort by the second column I used the same code just replacing the 1 with a 2. My question is: Do I have to do that since the second column is already a string?

Chmical

Oleg Komarov

unread,
Mar 12, 2010, 12:52:08 PM3/12/10
to
> > sort by first col (trsnforming num2str).
> > sortrows(cellfun(@num2str, test(2:end,:),'un',0),1)
> >
> > If you have the statistics tb use the dataset object.
> > Oleg
>
> Thank you Oleg works great. One more question... If I sort by the second column I used the same code just replacing the 1 with a 2. My question is: Do I have to do that since the second column is already a string?
>
> Chmical

The point is that sort and sortrows accept only numeric inputs or cell arrays of strings.
There are other ways to sort a cell array, the best woul be to use a dataset object:

dt = dataset({[test{2:end,1}].','PC'},{test(2:end,2),'S'},{test(2:end,3),'PO'})
ans =

PC S PO
84938 'O' 'Q34ef3'

84248 'O' 'Q37h93'
84548 'P' 'Q34g53'
sortrows(dt,'S')

Otherwise you can extract the column PC (only its values) convert to double, sort it, take the idx and use it to sort te wole test cell array.

Oleg

Chmical

unread,
Mar 12, 2010, 1:27:24 PM3/12/10
to
> >
> > Thank you Oleg works great. One more question... If I sort by the second column I used the same code just replacing the 1 with a 2. My question is: Do I have to do that since the second column is already a string?
> >
> > Chmical
>
> The point is that sort and sortrows accept only numeric inputs or cell arrays of strings.
> There are other ways to sort a cell array, the best woul be to use a dataset object:
>
> dt = dataset({[test{2:end,1}].','PC'},{test(2:end,2),'S'},{test(2:end,3),'PO'})
> ans =
> PC S PO
> 84938 'O' 'Q34ef3'
> 84248 'O' 'Q37h93'
> 84548 'P' 'Q34g53'
> sortrows(dt,'S')
>
> Otherwise you can extract the column PC (only its values) convert to double, sort it, take the idx and use it to sort te wole test cell array.
>
> Oleg

Thank you again for the help...

0 new messages