File Exchange

image thumbnail

Sorting a Cell Array

version 1.0.0.0 (2.43 KB) by Jeff Jackson
This function will sort a cell array even if it is mixed.

5 Downloads

Updated 05 Mar 2008

View License

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.

Cite As

Jeff Jackson (2021). Sorting a Cell Array (https://www.mathworks.com/matlabcentral/fileexchange/13770-sorting-a-cell-array), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (34)

PHK

smsinks

Perfect

Jeff Jackson

Hi Aaron,

I recommend that you use the MATLAB "flipud" function after using my "sortcell" function to flip the resulting matrix, if you want the cell array in descending order.

Cheers.

Aaron

Hi. How do people access the update (which enables descending order)? The download file was last updated in 2007.
Cheers Aaron

Farzin

Stephen Cobeldick

Stephen Cobeldick

MATLAB's standard "sortrows" function provides identical functionality (sorting a cell array with columns of strings and/or columns of numerics), but also allows these to be any class of numeric, not just 'double' like in this submission. In fact "sortrows" also sorts plain-old numeric and char arrays, which is a significant advantage over this submission. Some people may appreciate that this submission is a little faster than "sortrows", although this does not seem to be intentional. This submission has reasonably tidy code, though a few things are needed to make it really useful to use:
- The H1 line should be more descriptive (e.g.: 'sort the rows of a cell array containing columns of numeric or char data').
- At least one complete (working) example, preferably more.
- A list of functions in a 'See Also' line.
Some points detract also from the quality of the output:
- Accepts an N-D cell array, but reshapes this to return a 2-D cell array with the same number of rows as the input array.
- Does not return the sort indices.
- Though the documentation says that it works with 'mixed cell arrays containing character strings and numeric values', in fact each column must contain either char or numeric data exclusively, just like "sortrows" does: it really would be a significant advantage over "sortrows" if true mixed sorting had been implemented in this submission.

Although the function (mostly) does what is claims to do, there seems little advantage to using this submission over the standard MATLAB function "sortrows", which provides the same functionality over a wider range of data classes.

Jessica also observed in her comment that this submission does not provide a natural-order sort. Several of my submissions provide natural-order sorting, e.g. for sorting the rows of a cell of strings:
http://www.mathworks.com/matlabcentral/fileexchange/47433
Or for sorting a cell array of filenames:
http://www.mathworks.com/matlabcentral/fileexchange/47434
Both of these require this customizable natural-order sort function:
http://www.mathworks.com/matlabcentral/fileexchange/34464

James

Matlab function: sortrows() does this.

Mohit

Thanks a lot!

David Barry

Excellent.

Alex

David

Works great! Excellent job!

Ang Li

Good job!

Saeid

Good job.
I use uigetfile to load a number of images (say, image001 to image078) in my code. When I run my code, I click on image001, push shift key, and then click on image0078. As a result in the uigetfile cell array I get this order: image078, Image001, image002, ..., image076, image077, which caused many terrible so far. Using sortcell, the problem is solved.
thanks

Patrice

Simple and efficient. Cheers bro

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!

Bob Hamans

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

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]);

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!

Simon Cichosz

Perfect

Leila

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

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

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

Jack B.

Great. Thanks !

Sachin Singh

T S

Thanks

Lars Christensen

Just what I was looking for. Thank you.

P. V.A.

Thanks a lot, you save my life! :)

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.

Florian Wamser

does exactly what I need; thank you

Omar Mukhtar

vasilis papadourakis

thank you very much , great function!!

Fabio Lamanna

FANTASTIC VERY VERY USEFUL!!!

Alejandro Endo

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

MATLAB Release Compatibility
Created with R2006b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!