How do I combine two cell arrays into one cell array?

551 views (last 30 days)
pengcheng on 13 Oct 2014
Answered: Josep Llobet on 1 Oct 2021
>> Q{1}
ans =
'4400002970000003533'
'4400002970000003533'
'4400002970000003535'
'4400002970000003536'
'4400002970000003533'
'4400002970000003532'
'4400002970000003537'
>> Q{2}
ans =
'4400002890000146180'
'4400002890000146180'
'4400002970000000026'
I want to get a new cell:
'4400002970000003533'
'4400002970000003533'
'4400002970000003535'
'4400002970000003536'
'4400002970000003533'
'4400002970000003532'
'4400002970000003537'
'4400002890000146180'
'4400002890000146180'
'4400002970000000026'
I don't want to use the function cell2mat, because it is too slow for my program. Do you have any good ideas?

Star Strider on 13 Oct 2014
To get the result cell array ‘R’, for instance, vertically concatanate ‘Q{1}’ and ‘Q{2}’:
Q{1} = ['4400002970000003533'
'4400002970000003533'
'4400002970000003535'
'4400002970000003536'
'4400002970000003533'
'4400002970000003532'
'4400002970000003537'];
Q{2} = ['4400002890000146180'
'4400002890000146180'
'4400002970000000026'];
R = {[Q{1}; Q{2}]};
celldisp(R) % Display Result
Star Strider on 13 Oct 2014
@Adam — The vertcat function is definitely the way to go!
You’re certainly correct on your observation — in more Questions that I care to count, the instance in the question may have little bearing on actual issue!

Chenchal on 3 Nov 2017
cell2mat(Q')

Josep Llobet on 1 Oct 2021
Maybe this little function could be useful:
function [celltot] = juntar_cells(cell1, cell2)
celltot = cell1;
for ll_cell2 = 1:length(cell2)
celltot{end + 1} = cell2{ll_cell2};
end
end