MATLAB: Sort a cell array of char

cell arrayscharsort

Hi,
I have a cell array of chars (some of the cells should be strings while other should be doubles). Now I am trying to sort several of the rows that should be doubles and I get, of course, a sort from the left of the number as : 1001, 112, 14.. Tried to convert to double with both writing sort(double(…)) and cellfun(@double, …,'Uniformoutput', false) and it won't accept: 1) ??? Error using ==> double Conversion to double from cell is not possible." 2)??? Error using ==> sort Input argument must be a cell array of strings. What can I do to get 14, 112, 1001… Thank you Keren

Best Answer

The question is not clear. This is a contradiction:
1. "cell array of chars" 2. "(some of the cells should be strings while other should be doubles)"
From the later text I guess, you want to sort a cell vector which contains scalar doubles:
C = {1001, 112, 14};
[dummy, index] = sort([C{:}]);
sortedC = C(index);
CELLFUN(@double, ...) converts each element of the cell to a DOUBLE - therefore it does no change anything here!
 
EDITED: Now a method to sort a cell string according to the numbers the strings represent:
C = {'1001', '112', '14'};
Str = sprintf('%s,', C{:});
D = sscanf(Str, '%g,');
[dummy, index] = sort(D);
sortedC = C(index);