MATLAB: Sorting cell arrays based on nested cell array

cell arraysMATLAB

I have a 5×5 cell array and the last cell column is nested cell array which contains double and I have to sort the whole cell array based on the double? is there way to do it?

Best Answer

score = {'Name', 'Score', 'Rating'; ...
'Messi', {{97}}, {[9.75]}; ...
'Ronaldo', {98}, {[9.8]}; ...
'Neymar', {{{{96}}}}, {[9.7]} };
This is your example in a more readable format. This is a really strange format. The best idea is to fix the code, which produces these data. The nesting of the cells is a shot in you knee.
If the code, which produces this, is not yours and you cannot improve it, cleanup the data at first:
function C = cleanMyCell(C)
for iC = 1:numel(C)
aC = C{iC};
while iscell(aC)
aC = aC{1};
end
C{iC} = aC;
end
end
After score=cleanMyCell(score) your data look like:
score = {'Name', 'Score', 'Rating'; ...
'Messi', 97, 9.75; ...
'Ronaldo', 98, 9.8; ...
'Neymar', 96, 9.7};
Now the sorting is easy:
Rating = [score{2:end, 3}];
[~, SortIndex] = sort(Rating);
score(2:end, :) = score(SortIndex + 1, :)