Search code examples
matlabstring-concatenationcell-array

Concatenate two cell arrays of strings with a space in between?


How can I concatenate:

A = {'hello'; 'hi'; 'hey'}

with

B = {'Ben'; 'Karen'; 'Lisa'}

with a space in between to get:

C = {'hello Ben'; 'hi Karen'; 'hey Lisa'}

Is there a fast non-looping way?


Solution

  • You can use strcat(), although it performs a loop:

    strcat(A,{' '}, B)
    

    where the blank is preserved by enclosing it within a cell.

    Alternatively, FEX:CStrCatStr is a mex routine which achieves a 10x speedup (depending on testing environment):

    CStrCatStr(A,' ', B)