Search code examples
stringmatlabcell-array

Deleting part of a strings-cell array


This is my sample data (cell array)

>A_

'CUST_1627_PI425479659'   'Pri-miR-100u2'         'u2'
'CUST_2430_PI425479649'   'Pri-miR-L7a-3d'        'd'
'CUST_226_PI425479649'    'Pri-miR-3130-4u1'      'u1'
'CUST_1618_PI425479649'   'Pri-miR-147bu'         'u'
'CUST_1449_PI425479659'   'Pri-miR-107u'          'u'
'CUST_1546_PI425479659'   'Pri-miR-4299d1'        'd1'

The last one character or two last characters in the second column are written in the third column. I would like to remove them from strings in second column.

In a result it should look like this

>A_

'CUST_1627_PI425479659'   'Pri-miR-100'       'u2'
'CUST_2430_PI425479649'   'Pri-miR-L7a-3'     'd'
'CUST_226_PI425479649'    'Pri-miR-3130-4'    'u2'
'CUST_1618_PI425479649'   'Pri-miR-147b'      'u'
'CUST_1449_PI425479659'   'Pri-miR-107'       'u'
'CUST_1546_PI425479659'   'Pri-miR-4299'      'd1'

I tried in this way but it doesn't work.

s= {'u','u1','u2','d','d1'};

for i=1:length(A_(:,2))
    A_(i,2)= erase(A_(i,2),s)
end

Solution

  • Use regexprep to replace the occurrences of the third column in the second column with ''.

    A_(:,2) = regexprep( A_(:,2), A_(:,3), '');
    

    or to fix your code which uses erase (introduced in R2016b):

    for k=1:length(A_(:,2))
        A_(k,2) = erase(A_(k,2), A_(k,3)); %You need A_(k,3) here
    end
    

    but... since erase is directly applicable on cell arrays, so you don't need a loop here i.e.

    A_(:,2) = erase(A_(:,2), A_(:,3));