Search code examples
arraysmatlabcellscell-array

Strcat cell array and array


I have a cell array with strings and a numeric array in Matlab. I want every entry of the cell array to be concatenated with the number from the corresponding position of the array. I think that it can be easily solved using cellfun, but I failed to get it to work.

To clarify, here is an example:

c = {'121' '324' '456' '453' '321'};
array = 1:5

I would like to get:

c = {'1121' '2324' '3456' '4453' '5321'}

Solution

  • A special version of sprintf that outputs directly to a cell array is called sprintfc:

    >> C = sprintfc('%d%d',[array(:) str2double(c(:))]).'
    C = 
        '1121'    '2324'    '3456'    '4453'    '5321'
    

    It is also a bit different in the way it handles array inputs, by preserving the shape.