Why do the res of 'regexp' return NULL (Matlab)

42 views Asked by At

I am trying to use the function regexp in Matlab. The first code is ok as follows:

          data={'ABCD' 'BCDE' 'ACBE' 'ADEBC '} % 3 AB, 2 BE, 2 BC

          %res = regexp( data, 'A.*.*B') % OK code

The above code is to count the A.*.B order in the data cell array. However, when I try to test with the celldata which is defined as follows:

            celldata = {'AB'; 'BE'; 'BC'}

            for kk=1:length(celldata)

                 res = regexp( data,'celldata{kk}(1).*.*celldata{kk}(2)')

            end 

The 'res' value return NULL . Please kindly where is the bug here .Could anyone please help me to count the order of each element in celldata in data ? Thanks !

1

There are 1 answers

3
rst On

Use the concatenation of strings

res = regexp( data, [celldata{kk}(1) '.*.*' celldata{kk}(2)])

to find the amount of occurences

for kk=1:length(celldata)
   res = cellfun(@(x) length(x), regexp( data,[celldata{kk}(1) '.*' celldata{kk}(2)]))
end