Search code examples
matlabcell

Extract a specific part of cell array values


I have a cell array with values similar to the following one

13:41:54.879

I would like to extract only 13:41 part of the given value and mitigate the rest. I tried various combinations of extractBefore() and extractAfter() but couldn't get it.


Solution

  • You can use a regular expression to match the pattern "digits, colon, digits":

    c = {'13:41:54.879', '1:22:33.45679'};
    result = regexp(c, '\d+:\d+', 'match', 'once');
    

    gives

    result =
      1×2 cell array
        {'13:41'}    {'1:22'}