MATLAB: Order a cell array by the dimension of its cells

arraycelllengthMATLABsort

Hi, I am looking for a simple and possibly clean way to sort the cells of a cell array by the length of the vector inside every cell. For instance I have this cell array:
c =
[1x3 double]
[1x4 double]
[1x2 double]
[1x3 double]
and i want to obtain this:
c =
[1x2 double]
[1x3 double]
[1x3 double]
[1x4 double]
As I said I was looking for something clean rather than fast, and I need working it just for array cells and not for matrix cells in general, thank you very much in advance.

Best Answer

Here is one way:
Build test data:
>> c = {randi(10,1,3), randi(10,1,4),randi(10,1,2),randi(10,1,3)}'
c =
[1x3 double]
[1x4 double]
[1x2 double]
[1x3 double]
Sort cells' content length, get indices of ordered lengths:
>> [~,id] = sort( cellfun( @length, c ))
id =
3
1
4
2
Use indices to sort original cell array:
>> c = c(id)
c =
[1x2 double]
[1x3 double]
[1x3 double]
[1x4 double]