Search code examples
stringmatlabmaxsegment

Matlab: find the length of longest substring in sequence


In matlab: I want to find the length of longest i-segment in the sequence for i ∈{H,E }.

Example: HHHCCEEECCCHHHHCCCCC
lengh of longest H-segment in sequence is: 4

How should i do this?


Solution

  • One-liner with regexp:

    max(cellfun(@numel, regexp(str, '([HE])\1+', 'match')))