Search code examples
arraysmatlabcellcell-array

Convert a cell array of number into cell array of strings in MATLAB


I have the cell array of number matrices as follows:

  c= {[1,2,3,4] [1,2,4,3]   [1,3,2,4]}

Denoted that 1=A, 2=B, 3=C,4=D.How can I convert c into the cell array of strings as follows?

 s= {[A,B,C,D]  [A,B,D,C]   [A,C,B,D]}

And how can we generalize this rule such as 1 to 7 and A to G ... ?


Solution

  • If you by [A,B,C,D] intend

    ['A','B','C','D'] => 'ABCD'
    

    that is, to concatenate all of them to a single string, you could add 64 to each number (to get the proper ASCII-coding) and covert the numbers to a char.

    s = cellfun(@(x) char(x + 64), c, 'UniformOutput', false);
    s = 
         'ABCD'    'ABDC'    'ACBD'