# Making cell array of cells of strings the same size by adding empty strings

1 view (last 30 days)
Cecile on 6 Jan 2020
Commented: Cecile on 6 Jan 2020
Hello,
I have a cell array of cells containing strings. The cells withing the cells have different length. I would like to write an empty string '' to make all the cells the same size.
Cell array of cells of string example:
c = {{'a1', 'B20', 'd_3'}; {'b44', 'C5', 'e_12', 'k234'}; {'a8', 'T565', 'V-d3'}; {'b44', 'C5', 'e_12', 'k234', 'k234', 'k234'}};
c =
4×1 cell array
{1×3 cell}
{1×4 cell}
{1×3 cell}
{1×6 cell}
len = cellfun('length', c)
len =
3
4
3
6
I need all the cells in c to be the same length, ie of length max(len) which is 6. I would like to add empty Strings '' from the last value to the 6th columns for all the cells that have a length that is less than 6.
so I would like to transform c into a cNew that would look like this this:
cNew = {{'a1', 'B20', 'd_3', '', '', ''}; {'b44', 'C5', 'e_12', 'k234', '', ''}; {'a8', 'T565', 'V-d3', '', '', ''}; {'b44', 'C5', 'e_12', 'k234', 'k234', 'k234'}};
cNew =
4×1 cell array
{1×6 cell}
{1×6 cell}
{1×6 cell}
{1×6 cell}
len = cellfun('length', cNew)
len =
6
6
6
6
If anyone could help I would be very grateful, thank you.
Best Regards;
Cecile

Guillaume on 6 Jan 2020
maxlen = max(cellfun(@numel, yourcellarray));
newcellarray = cellfun(@(s) [s, repmat({''}, 1, maxlen - numel(s))], yourcellarray, 'UniformOutput', false);
would be one way.

#### 1 Comment

Cecile on 6 Jan 2020
Thank you very much, that's exactly it.