1
votes

I have 3 cell arrays of some strings cell1, cell2 and cell3. I want to save them in one cell matrix.If the column size of each cell are col1 col2 and col3. I want to create one cell with size (max(col1,col2,col3)*3)).How can I?

cellmarix{:,1}=cell1;
cellmarix{:,2}=cell2;
cellmarix{:,3}=cell3;

but this created a cell of cells of size (1*3).

I also used

cellmatrix={cell1,cell2,cell3};

but the result was the same(1*3) cell of cells.

for example if I have

  cell1={
 'uzi'
 'julian'
 'ahyden'
 'kwayne'
 'riel'
 'gazook'
 'mustapha'
  }

cell2={
 'negro'
 'kris'
'sascha'
'jimw'
'andi'
'andrei'
 }

cell3={
'joncruz'
'youngsd'
'notzed'
'werner'
'cactus'
'Iain'
'faassen'
 }

The result is :

cell_all={
'uzi'        'negro'    'joncruz'
'julian'      'kris'    'youngsd'
'ahyden'      'sascha'  'notzed'
'kwayne'       jimw'    'werner'
'riel'        'andi'    'cactus'
'gazook'     'andrei'   'Iain'
'mustapha'      []      'faassen'
}
2
Can you provide an explicit example please? - Eitan T
What about cellmatrix={cell1;cell2;cell3}? - Dan
@Dan that would produce a cell of cells, apparently that's not what the OP wants. - Eitan T
@EitanT Thanks for comment. I add an example. - Fatime
@Dan Thanks. I add an example. - Fatime

2 Answers

2
votes

You can do the following:

cell_all = cell1;
cell_all(1:numel(cell2), 2) = cell2;
cell_all(1:numel(cell3), 3) = cell3;

If you have a lot of cells (like you say you do), you can resort to a loop:

n = 3; %// Number of columns
cell_all = cell1;
for k = 2:n
    varname = sprintf('cell%d', k);
    cell_all(1:numel(eval(varname)), 2) = eval(varname);
end

This is one of those rare cases where eval actually helps. However, I cannot help but wonder why there are so many cell variables in your workspace instead of populating one large cell array right from the start.

1
votes
C = {'one','two','three'};
str = strjoin(C)

--> see:

TMW: join cell