将字符串与元胞数组中的每个字符串组合
combine string with each and every string in a cell array
我有像下面这样充满字符串的元胞数组
a =
'one' 'two'
'three' 'four'
现在我将上面的单元格数组 a 分配给另一个单元格数组 b 第一,第二和第三位如下所示
b{1} =a;
b{2} =a;
b{3} =a;
现在我想将字符串 X = '-h';
与 b 元胞数组
的每个字符串合并
我该怎么办?
示例输出为
b =
{2x2 cell}
{2x2 cell}
{2x2 cell};
b{1} ={'one-h' 'two-h' ;'three-h' 'four-h'};
b{2} ={'one-h' 'two-h' ;'three-h' 'four-h'};
b{3} ={'one-h' 'two-h' ;'three-h' 'four-h'};
但在将 a 值分配给 b 后我需要此输出,如步骤 2 (b{1} = a
...) 并且字符串 X 必须与 [=27= 组合]b 仅限元胞数组
试试这个:
%// Using nested cellfun
b = cellfun(@(x) cellfun(@(y) strcat(y,'-h'),x,'Uni',0),b,'Uni',0);
输出:
>> b{1}
ans =
'one-h' 'two-h'
'three-h' 'four-h'
我有像下面这样充满字符串的元胞数组
a =
'one' 'two'
'three' 'four'
现在我将上面的单元格数组 a 分配给另一个单元格数组 b 第一,第二和第三位如下所示
b{1} =a;
b{2} =a;
b{3} =a;
现在我想将字符串 X = '-h';
与 b 元胞数组
我该怎么办?
示例输出为
b =
{2x2 cell}
{2x2 cell}
{2x2 cell};
b{1} ={'one-h' 'two-h' ;'three-h' 'four-h'};
b{2} ={'one-h' 'two-h' ;'three-h' 'four-h'};
b{3} ={'one-h' 'two-h' ;'three-h' 'four-h'};
但在将 a 值分配给 b 后我需要此输出,如步骤 2 (b{1} = a
...) 并且字符串 X 必须与 [=27= 组合]b 仅限元胞数组
试试这个:
%// Using nested cellfun
b = cellfun(@(x) cellfun(@(y) strcat(y,'-h'),x,'Uni',0),b,'Uni',0);
输出:
>> b{1}
ans =
'one-h' 'two-h'
'three-h' 'four-h'