Adding numerical array to cell array

1 vue (au cours des 30 derniers jours)
Saeid
Saeid le 27 Nov 2017
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?

Réponse acceptée

Star Strider
Star Strider le 27 Nov 2017
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'}

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by