append string to each element in string cell array
260 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
その他の回答 (1 件)
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 件のコメント
Sean de Wolski
2011 年 10 月 14 日
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'].'
Sean de Wolski
2011 年 10 月 14 日
And it's not an inline function but a function handle; significantly faster and in current times more common.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!