I am having an issue concatenating two cell arrays.

조회 수: 3 (최근 30일)
Kellie Anton
Kellie Anton 2017년 8월 29일
댓글: KL 2017년 8월 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?

채택된 답변

KL
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
Jan 2017년 8월 29일
voltqtr = (cellfun(@(a,b) [a '-' b],vclassM,YearQtrM,'UniformOutput',false))';
can be replaced by the easier:
strcat(vclassM, '-', YearQtrM)
KL
KL 2017년 8월 30일
Thanks Jan.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by