Adding numerical array to cell array
11 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Star Strider
on 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'}
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!