Code covered by the BSD License  

Highlights from
Sorting a Cell Array

4.85714

4.9 | 14 ratings Rate this file 97 Downloads (last 30 days) File Size: 2.43 KB File ID: #13770

Sorting a Cell Array

by Jeff Jackson

 

25 Jan 2007 (Updated 05 Mar 2008)

This function will sort a cell array even if it is mixed.

| Watch this File

File Information
Description

This function sorts a cell array, even if it contains character strings and numeric values (i.e. mixed). The only restriction is that it assumes that each column contains either numeric values or character strings, not both. It sorts the rows in ascending order. Note that it allows more than one column to be sorted according to the priority input by the user.

MATLAB release MATLAB 7.3 (R2006b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (19)
03 Aug 2007 Alejandro Endo

great useful function. Thanks for writing it, it saved me some time

19 Sep 2007 Fabio Lamanna

FANTASTIC VERY VERY USEFUL!!!

12 Nov 2007 vasilis papadourakis

thank you very much , great function!!

16 Dec 2007 Omar Mukhtar  
08 Feb 2008 Florian Wamser

does exactly what I need; thank you

04 Mar 2008 J Ahmed

Works well for sorting in one dimension. Produces errors in you try to order with two or more dimensions. TO fix this, edit line 77 so that it reads:
X = sortcell(X, col,MODE);

Also, if two dimensions are used, the order is not preserved from the first sort to the second, which is very confusing.

06 May 2008 P. V.A.

Thanks a lot, you save my life! :)

10 May 2008 Lars Christensen

Just what I was looking for. Thank you.

18 Jul 2008 T S

Thanks

07 Jul 2010 Sachin Singh  
10 Sep 2010 Jack B.

Great. Thanks !

15 Sep 2010 Sachin Singh

Folks I tried following to re arrange the data in desending way.
NewCellArray={};
for i = 1: length(SortedData)
    NewCellArray(k,:) = SortedData(length(SortedData)+1-i,:);
    k=k+1;
end

05 Jan 2011 jayesh

 i have
m=100;
for m=1:100;
  for k1=1:1:22
         filearray(m,k1)=filename(k1);
end
end
here k1 is filename of image so total Nfiles are 100
then
 for i=1:1:Nfiles
             if(A(i)<15.8)
                 for k1=1:1:22
                    filename(k1)=filearray(i,k1);
                 end
                  imshow(filename);
                 disp(A(i));
                
          end
 end
A(i)is the value which is corresponding to the file name i want to sort filename with respect to A(i)
what i can do
above code show it in serial that means from 1 to 100 without sorting

07 Feb 2011 Leila

Great, it worked. Thanks, it saved me a lot of time!

14 Feb 2011 Simon Cichosz

Perfect

01 Jun 2011 Bob Hamans

Would there be an easy way of getting the overall permutation vector for a multidimensional sort? For one dimension it is easy (just return ix defined at line 108) but when using multiple dimension only the last value of ix is returned. I was thinking to implement something like ix(ix) but that of course does not work. Suggestions welcome!

01 Jun 2011 Bob Hamans

Solved for now. Maybe I will update this function at a future time point to implement the above. For now:

[data, ixa] = sortcell(data, 11);
[data, ixb] = sortcell(data, 12);
[data, ixc] = sortcell(data, 1);
[data, ixd] = sortcell(data, 5);
ix=ixa(ixb(ixc(ixd)));

Desired would be:
[data, ix] = sortcell(data, [11 12 1 5]);

02 Jun 2011 Bob Hamans

Correct that! The last line should read:
 ix = ixin(ix);

24 Oct 2011 Jessica

Just a note to anyone who wanted to use this for the same purpose as me.

If you have a cell array of strings, but want to sort by a column that contains numeric values as class string (i.e.

'0.50'
'1.15'
'10.2'
'100'
'20')

sort is notorious for treating strings a character at a time. So, if I used this function on the above vector, 20 would be sorted after 100 on account of the character 1 coming before 2.

I understand that this function would work wonderfully if that entire column was a number instead of a string containing a number, but depending on the application it may not be possible to force the class to double. In my case, I don't know if that column is going to contain strings or numbers until I need to sort it, so I ended up going my own route.

Very useful program though, thanks for sharing on FEX!

Please login to add a comment or rating.
Updates
26 Jan 2007

I uploaded a previous bugged version, not the final one, sorry folks.

03 Mar 2008

This script was updated to add the functionality for sorting according to the desired user specified direction (ascending or descending).

05 Mar 2008

The update for the MODE functionality does not work correctly as Mathworks sort.m function does not allow cell arrays to be sorted when using the DIM argument.

Tag Activity for this File
Tag Applied By Date/Time
structures Jeff Jackson 22 Oct 2008 08:58:19
cell arrays Jeff Jackson 22 Oct 2008 08:58:19
sort cell array Jeff Jackson 22 Oct 2008 08:58:19
cell arrays Enkhbayar Batzorig 19 Aug 2009 05:10:29
structures Enkhbayar Batzorig 19 Aug 2009 05:10:31
sort cell array Enkhbayar Batzorig 26 Aug 2009 07:22:07
cell arrays Ramy Sukarieh 10 Sep 2009 02:45:43
cell arrays N 11 Jul 2010 09:05:25
sort cell array N 11 Jul 2010 09:05:28
sortcellarray Sachin Singh 15 Sep 2010 05:05:04
sort cell array Javier Pacheco Labrador 27 Sep 2011 06:49:36

Contact us at files@mathworks.com