0

I have the cell array of strings in matlab. I want to sort letters in every string in alphabetical order. How can I do that?

For example, if I have ['dcb','aetk','acb'}], I want it to be: ['bcd','aekt','abc'].

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
Macaronnos
  • 647
  • 5
  • 14
  • Sort each string individually, or sort all the letters of all the strings into one output string? A brief example of the input and desired output would be helpful. – Notlikethat Mar 09 '14 at 15:32

1 Answers1

3

The handy helper here is cellfun, with the correct option for nonscalar output - we tell it to run sort on each element of the cell array in turn:

>> a = {'dcb' 'aetk' 'acb'}
a =
{
  [1,1] = dcb
  [1,2] = aetk
  [1,3] = acb
}

>> b = cellfun(@sort, a, 'UniformOutput', false);
b =
{
  [1,1] = bcd
  [1,2] = aekt
  [1,3] = abc
}
Notlikethat
  • 20,095
  • 3
  • 40
  • 77