I am having an issue concatenating two cell arrays.

3 views (last 30 days)
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?

Accepted Answer

KL
KL on 29 Aug 2017
Edited: KL on 29 Aug 2017
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 Comments
Jan
Jan on 29 Aug 2017
voltqtr = (cellfun(@(a,b) [a '-' b],vclassM,YearQtrM,'UniformOutput',false))';
can be replaced by the easier:
strcat(vclassM, '-', YearQtrM)

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 29 Aug 2017
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

Categories

Find more on Cell Arrays 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!