I am having an issue concatenating two cell arrays.

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?

 Réponse acceptée

KL
KL le 29 Août 2017
Modifié(e) : KL le 29 Août 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 commentaires

Ok. Thank you. So, why does this work?
KL
KL le 29 Août 2017
Modifié(e) : KL le 29 Août 2017
First we create some dummy data
vclass = {'100kV','150kV','600kV'};
YearQtr = {'2014 Q1','2014 Q2'};
Then we create some mesh of each cell array so we could simply concatenate them element by element to get what we want
[vclassM,YearQtrM] = meshgrid(vclass,YearQtr)
vclassM =
2×3 cell array
'100kV' '150kV' '600kV'
'100kV' '150kV' '600kV'
YearQtrM =
2×3 cell array
'2014 Q1' '2014 Q1' '2014 Q1'
'2014 Q2' '2014 Q2' '2014 Q2'
then we use cellfun. Cellfun applies the function element by element. Our function here is to concatenate the elements and put a - between them, hence [a '-' b], here a and b are elements of the our cellarray that's passed onto the cellfun vclassM and YearQtrM.
voltqtr = (cellfun(@(a,b) [a '-' b],vclassM,YearQtrM,'UniformOutput',false))';
Finally we just make them as a column matrix as you wish.
voltqtr = voltqtr(:)
You changed it?
KL
KL le 29 Août 2017
hmm.. which part? I transposed the cellfun output so the result looks similar to how've you described it in your question but I didn't change anything functionally!|
voltqtr = (cellfun(@(a,b) [a '-' b],vclassM,YearQtrM,'UniformOutput',false))';
can be replaced by the easier:
strcat(vclassM, '-', YearQtrM)
KL
KL le 30 Août 2017
Thanks Jan.

Connectez-vous pour commenter.

Plus de réponses (1)

Guillaume
Guillaume le 29 Août 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

Catégories

Question posée :

le 29 Août 2017

Commenté :

KL
le 30 Août 2017

Community Treasure Hunt

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

Start Hunting!

Translated by