How can I merge multiple cells in one row?

조회 수: 5 (최근 30일)
Vilém Frynta
Vilém Frynta 2022년 4월 30일
댓글: Star Strider 2022년 4월 30일
I have 2 cells. I need to put them both in a first row. First cell will be first, and the second cell will repeat n times after the first one, in first row.
For better imagination:
cell_1 = {'date', 'year', 'place'}
cell_2 = {'name', 'age', 'time'}
And the final result should be one-row cell with cell_1 and multiple cell_2 following after.
Like this:
firstrow = {'date', 'year', 'place','1_name', '1_age', '1_time','2_name', '2_age', '2_time','3_name', '3_age', '3_time'}
I've tried this already with for/while loops to go step by step and put all the cell content in the table, however it isn't perect and it keep skipping last cells. And, I was unsuccesful with using things like "cell_1{end:end+3} = cell_2{:}".
Thanks in advance.
edit: better choice of words

채택된 답변

Star Strider
Star Strider 2022년 4월 30일
Try this —
cell_1 = {'date', 'year', 'place'};
cell_2 = {'name', 'age', 'time'};
firstpass = cellfun(@(x)sprintf('%%d_%s',x),cell_2, 'Unif',0);
for k1 = 1:numel(firstpass)
for k2 = 1:3
sp{k1,k2} = sprintf(firstpass{k1},k2);
end
end
secondpass = reshape(sp, 1, []);
firstrow = cat(2, cell_1, secondpass)
firstrow = 1×12 cell array
{'date'} {'year'} {'place'} {'1_name'} {'1_age'} {'1_time'} {'2_name'} {'2_age'} {'2_time'} {'3_name'} {'3_age'} {'3_time'}
.
  댓글 수: 2
Vilém Frynta
Vilém Frynta 2022년 4월 30일
Thank you Star Strider! It works very well!
Cool profile pic by the way ÷)
Star Strider
Star Strider 2022년 4월 30일
As always, my pleasure!
Thank you (from me and my polar bear)!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by