Create new column in cell array based on string values in other columns

13 ビュー (過去 30 日間)
phlie
phlie 2016 年 8 月 16 日
コメント済み: phlie 2016 年 8 月 16 日
In my cell array, I have three columns containing strings: 'string1' 'string2' 'string3'
I would like to add a new column containing a combination of the other three strings (separated by '_'): 'string1_string2_string3'
How can I do this?

採用された回答

Guillaume
Guillaume 2016 年 8 月 16 日
Use strjoin to merge the strings and a loop to go over the rows:
C = {'aaa', 'b', 'cccc';
'string1', 'string2', 'string3'}
newcol = size(C, 2) + 1;
for row = 1 : size(C, 1)
C{newcol, 4} = strjoin(C(row, :), '_');
end
Or using cellfun instead of a loop:
newC = cellfun(@(row) [row, strjoin(row, '_')], num2cell(C, 2), 'UniformOutput', false)
newC = vertcat(newC{:})

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by