Search code examples
matlabreversecell-array

Reverse characters of each element in a cell array (Matlab)


I have a cell array as follows:

    celldata = {'AB'; 'BE'; 'BC'}

How can I create a cell array which characters of each element in a cell array 'celldata' are reversed. The output should be as follows:

    recelldata = {'BA'; 'EB'; 'CB'}

Thanks!


Solution

  • you could use fliplr, but it operates on each index of the cell instead of the whole cell. To wrap it all in one line, use cellfun

    recelldata = cellfun(@(x) fliplr(x), celldata,'UniformOutput', false)
    
    >>'BA'
      'EB'
      'CB'