Fatime
Fatime

Reputation: 197

Matlab, Create one cell of some cells

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'
}

Upvotes: 1

Views: 92

Answers (2)

Eitan T
Eitan T

Reputation: 32930

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.

Upvotes: 2

Lucius II.
Lucius II.

Reputation: 1832

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

--> see:

TMW: join cell

Upvotes: 1

Related Questions