I am having an issue concatenating two cell arrays.
3 次查看(过去 30 天)
显示 更早的评论
I have been trying several things that I have found in this forum, but I am obviously missing something. I have two cell arrays, one of size 7 and one of size 13 that I want to concatenate into a 91x1. More specifically, I have one that looks something like:
vclass = {100kV, 150kV,...600kV} YearQtr = {2014 Q1, 2014 Q2, ..., 2017 Q1}
I want to create one that looks like:
voltqtr = {100kV -2014 Q1, 150kV -2014 Q1,..., 600kV -2017 Q1}
I have tried: voltqtrs = strcat(repmat(vclass,sy,1),' - ',repmat(YearQtr',1,sv));
where sy is the row count of YearQtr (13 in this case) and sv is the row count of vclass (7 in this case) and get: Error using cell/strcat (line 44) All nonscalar inputs must be the same size.
I've tried: for i = 1:s for ii = 1:sv for j = 1:sy voltqtrs(i) = strcat(vclass2(ii),' - ',YearQutr(j)); end end end;
and get: Conversion to double from cell is not possible.
What am I doing wrong or missing?
0 个评论
采纳的回答
KL
2017-8-29
编辑:KL
2017-8-29
vclass = {'100kV','150kV','600kV'};
YearQtr = {'2014 Q1','2014 Q2'};
[vclassM,YearQtrM] = meshgrid(vclass,YearQtr);
voltqtr = (cellfun(@(a,b) [a '-' b],vclassM,YearQtrM,'UniformOutput',false))';
voltqtr = voltqtr(:)
6 个评论
Jan
2017-8-29
voltqtr = (cellfun(@(a,b) [a '-' b],vclassM,YearQtrM,'UniformOutput',false))';
can be replaced by the easier:
strcat(vclassM, '-', YearQtrM)
更多回答(1 个)
Guillaume
2017-8-29
A simpler method to the accepted answer, using the newly introduced string class (R2016b or later):
vclass = {'100kV','150kV','600kV'};
YearQtr = {'2014 Q1','2014 Q2'};
result = string(vclass) + '-' + string(YearQtr)'
result = cellstr(result(:)) %if a cell array is desired as output
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!