Concatenate multiple cell into a single cell

6 views (last 30 days)
Hi all,
I have cell (many cell) array that contains varying length in row direction. For example see below ilustration:
ncell1={'12','21','44','02','35'}; % 1x5 cell
ncell2={'03','21','45','45','11'}; % 1x5 cell
ncell3={'23','15','47','36','35','78'}; % 1x6 cell
ncell4={'47','28','99'}; % 1x3 cell
ncell={ncell1 ncell2 ncell3 ncell4}.'; %-->4x1 cell
the problem, how can i concatenate 4 cells those to 1 cell with 4x6 cell dimension size (yes, 4x6 size! not 4x1 or 1x4 or 1x1). So the result what i want like (note: the ncell5 below just copy paste ncell1-4 for easy ilustration) :
so far have i do:
result = cat(1,ncell); %same with result ncell
result = {[ncell(1) ncell(2) .... ]} %only combine the dimensions of the cells
result = [char(ncell{1}), char(ncell{2}) ....]; %character not uniform
remember i work with hundred ncell. tks for your help :)

Accepted Answer

Voss
Voss on 3 Jun 2022
Edited: Voss on 3 Jun 2022
ncell1={'12','21','44','02','35'}; % 1x5 cell
ncell2={'03','21','45','45','11'}; % 1x5 cell
ncell3={'23','15','47','36','35','78'}; % 1x6 cell
ncell4={'47','28','99'}; % 1x3 cell
ncell={ncell1; ncell2; ncell3; ncell4}; %-->4x1 cell
m = numel(ncell);
n = cellfun(@numel,ncell); % n = [5; 5; 6; 3]
ncell2d = cell(m,max(n)); % 4x6 cell (!)
for ii = 1:m
ncell2d(ii,1:n(ii)) = ncell{ii};
end
disp(ncell2d);
{'12'} {'21'} {'44'} {'02' } {'35' } {0×0 double} {'03'} {'21'} {'45'} {'45' } {'11' } {0×0 double} {'23'} {'15'} {'47'} {'36' } {'35' } {'78' } {'47'} {'28'} {'99'} {0×0 double} {0×0 double} {0×0 double}
  2 Comments
Voss
Voss on 4 Jun 2022
("Answer" from @eko supriyadi moved here:)
wow brilliant..thank you for your effort..
ES.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!