필터 지우기
필터 지우기

Concatenate Cell Arrays (string and numeric)

조회 수: 54 (최근 30일)
Jason
Jason 2012년 7월 13일
Hi all, I have two cell arrays: tempName & tempWeightSTR. 1. tempName was populated with strings. 2. tempWeightSTR started out as tempWeight which was numeric and I used num2cell to convert it to a cell array.
Is there a way to concatenate the two arrays so the resulting elements look like: ['Paul (20%)', 'John (24%)', 'Ringo (5%)']
I need my results to be one vector, i.e. ['A(1%)','B(2%)','C(3%)']... not [ 'A', 'B', 'C' '1%', '2%', '3%']
I tried strcat(tempName,' (', tempWeightSTR,')') but the numeric values do not show up for some reason.
Also, can someone please point me in the right direction to format the numeric values to string percentages (%)?
Thanks in advance! jason

채택된 답변

Nirmal
Nirmal 2012년 7월 13일
편집: Nirmal 2012년 7월 13일
a={'john','harry'};
b={'20','30'};
s='%'
for i=1:2
bb=cell2mat(b(1,i));
str=sprintf('(%s%s)',bb,s);
a(1,i)=strcat(a(1,i),str);
end
a
  댓글 수: 2
Jason
Jason 2012년 7월 13일
편집: Jason 2012년 7월 13일
Thanks Nirmal, 2 things.
1. Using the expression below the ans is 3.300000e-002%. I need 3.3%. sprintf('(%s%s)', 0.033,'%')
2. Is there a way to do all of this without a loop? I'd prefer a single expression.
Either way, thank you very much for your help! jason
Jason
Jason 2012년 7월 13일
I figured out #1 '%0.2f%s' = 3.30%
However, I'd still like to avoid using a loop. Thanks! jason

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

추가 답변 (1개)

Nirmal
Nirmal 2012년 7월 13일
a={'john','harry'};
b={'20','30'};
a=strcat(a,'(',b,'%)');

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by