Search code examples
matlabcell-array

combine string with each and every string in a cell array


I have cell array with full of string like below

a = 

    'one'      'two' 
    'three'    'four'

now I am assign the above cell array a to another cell array b first , second and third place Like below

b{1} =a;
 b{2} =a;
 b{3} =a;

now i want to combine the string X = '-h'; with each and every string of b cell array

How can I do ?

Example output is

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'};

but I need this output after assign the a value to b like step 2 (b{1} = a...) and string X must combine with b cell array only


Solution

  • Try this:

    %// Using nested cellfun
    b = cellfun(@(x) cellfun(@(y) strcat(y,'-h'),x,'Uni',0),b,'Uni',0);
    

    Output:

    >> b{1}
    
    ans = 
    
    'one-h'      'two-h' 
    'three-h'    'four-h'