append string to each element in string cell array

x_label =
'8' '16' '24' '32' '40'
How can I append ' GHz' to each element in the cell array? Thanks! Can it only be done with a for loop?

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 10월 14일

16 개 추천

x_label = {'8' '16' '24' '32' '40'};
strcat(x_label,'GHz')

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2011년 10월 14일
x_label = { '8' '16' '24' '32' '40'}
x_labelGHz = cellfun(@(c)[c 'GHz'],x_label,'uni',false)
One way

댓글 수: 3

Fangjun's algorithm is simpler, but I am curious about how yours works.
The function you are using is:
@(c)[c 'GHz'] ?
an inline function of variable c? And I guess cellfun will set c = each element in x_label?
yes, cellfun is basically a for loop that does the function to each cell.
The @(c)[c 'GHz'] takes a cell, c, and concatenates it horizontally with 'GHz'. I agree Fangjun's method is better for this case, I don't think I've ever used strcat before. cellfun is more powerful for other things. Perhaps if you wanted the strings as column vectors:
@(c)[c 'GHz'].'
And it's not an inline function but a function handle; significantly faster and in current times more common.

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

카테고리

도움말 센터File 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