Quantcast
Channel: MATLAB Central Newsreader Recent Posts
Viewing all articles
Browse latest Browse all 19628

Splitting a string using regexp

$
0
0
Hi all,

I have text data in the format:
str = '13/12/11 14:07:40.228322'
or it can be in the format:
str = '14:07:40.228322 13/12/11'

I want to put ONLY the 14, 07 and 40.228322 into a 1x3 cell array for further processing. To do this I have used:
[token remain] strtok(str, ' ');
[first, loc1] = regexp(token, ':', 'split');
[second, loc2] = regexp(remain, ':', 'split');

if ~isempty(loc1)
  timeCell = first;
else if ~isempty(loc2)
  timeCell = second
else
  timeCell = [];
end

Is there a neater way of doing this, one or two lines?

Best Regards
Stu

Viewing all articles
Browse latest Browse all 19628

Trending Articles