Problem with strcmp function in cell array

I am using cell array a with value as shown below

a = {'one' , 'two' ; 'four','five'};

now i just compare a to some string like 'two' after which i just add another column to a and insert the string at that place

a{strcmp(a,'two'),3} ='Three' ; 

      

I am getting output as shown below

 a = 

     'one'     'two'          []
     'four'    'five'         []
         []        []    'Three'

      

but actually i want the output as below

 a = 

     'one'     'two'     'Three'
     'four'    'five'         []

      

How can i do this?

+3


source to share


1 answer


Have you considered maps for your task?

%// create map
keySet =   {'one', 'two', 'three', 'four', 'five', 'six','seven','eight'};
valueSet = [1, 2, 3, 4, 5, 6, 7 ,8];
mapObj = containers.Map(keySet,valueSet);

%// data
a = {'one' , 'two' ; 'four', 'five'};

%// analyze data
Nums = cell2mat(values(mapObj,a));

%// expand data
Nums(:,3) = Nums(:,2) + 1

%// output
output = keySet(Nums)

      




output = 

    'one'     'two'     'three'
    'four'    'five'    'six' 

      

+2


source







All Articles