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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by