Adding numerical array to cell array

조회 수: 1 (최근 30일)
Saeid
Saeid 2017년 11월 27일
답변: Star Strider 2017년 11월 27일
I have a cell array of the form:
C={'Tom', 'Student', 'Jim', 'Faculty', 'Clare', 'Student'}
and a numerical array of the form:
N=[100 120 140 160 180 200]
How can I create a new cell array where N is placed somewhere inbetweem the elements of C, e.g.
C={'Tom', 'Student', '100' '120', '140', '160', '180', '200', 'Jim', 'Faculty', 'Clare', 'Student'}
I tried this:
N=strsplit(num2str([100 120 140 160 180 200]))
C={'Tom', N, 'Student', 'Jim', 'Faculty', 'Clare', 'Student'}
xlswrite('tempp.xls',C)
But the output is:
N =
1×6 cell array
'100' '120' '140' '160' '180' '200'
C =
1×7 cell array
'Tom' {1×6 cell} 'Student' 'Jim' 'Faculty' 'Clare' 'Student'
What I am doing wrong?

채택된 답변

Star Strider
Star Strider 2017년 11월 27일
Try this:
C={'Tom', 'Student', 'Jim', 'Faculty', 'Clare', 'Student'};
N=[100 120 140 160 180 200];
Nc = num2cell(N);
C = {C{1:2} Nc{:} C{3:end}};
Another option:
C={'Tom', 'Student', 'Jim', 'Faculty', 'Clare', 'Student'};
N=[100 120 140 160 180 200];
Ns = regexp(sprintf('%d\n',N), '\n', 'split');
C = {C{1:2} Ns{1:end-1} C{3:end}}
C =
1×12 cell array
Columns 1 through 7
{'Tom'} {'Student'} {'100'} {'120'} {'140'} {'160'} {'180'}
Columns 8 through 12
{'200'} {'Jim'} {'Faculty'} {'Clare'} {'Student'}

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by