0

I have a cell array:

cellArray = {
   '123' 'BC' 'other value';
   '124' 'BC' 'other value';
   '125' 'BC' 'other value';
   '126' 'BC' 'other value';
}

I would like to obtain this:

cellArray = {
   '123 BC' 'other value';
   '124 BC' 'other value';
   '125 BC' 'other value';
   '126 BC' 'other value';
}

As you can see the second column is now concatenated to the first... Any suggestion?

1 Answers1

4

Looks like strcat plus standard cell array concatenation can do it:

x = [strcat(cellArray(:,1), {' '}, cellArray(:,2)) cellArray(:,3)]

The only trick is that the middle space character needs to be in a cell, otherwise strcat tries to "help" by removing trailing spaces. See help strcat for an explanation.

Pursuit
  • 12,285
  • 1
  • 25
  • 41
  • Pursuit, how is possible that `x = [ ... ]` returns a cell array? `[]` shouldn't return a simple array? – dynamic Apr 04 '13 at 20:18
  • The syntax `x = [...]` is used to concatenate objects of the same type, preserving type. (This works for numeric arrays, logical arrays, arrays of structures, arrays of objects, and cell arrays.) Since the starting contents are cell arrays, the output is a cell array. Using `{...}` would create a nested cell array. (Useful at times, but not for this question.) – Pursuit Apr 04 '13 at 20:25