Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

How Do I Breakout and Store 2 Time Values In The Same String?

Asked by Brad

Brad (view profile)

on 18 Jun 2013

I’ve got a text file that contains hundreds of state times such as these:

State Time: 12:00:00.123 (43200.123)



State Time: 12:00:01.456 (43200.456)



State Time: 12:00:02.789 (43200.789)

I’m using the following commands to read the text file and define the pattern required for the REGEXP function:

buffer = fileread(OAMfilename);

exp = 'State Time:\s+([\d\:\.+\s+(\d.+)]+)';

ST = regexp(buffer, exp,'tokens');

This results in the following '<1x2 cell>':

'12:00:00.123 (43200.123)' '12:00:01.000 (43201.456)'

My goal is to have the 24-hour time and UTC times stored in separate cells (with no parentheses) for future processing/plotting purposes. Is this possible using my current approach?



Brad (view profile)



1 Answer

Answer by Kelly Kearney

Kelly Kearney (view profile)

on 18 Jun 2013
Accepted answer

In order to capture different parts of an expression, put those parts in parentheses (note in the example below the difference between \(, which matches a parentheses, and plain (, which begins a group and token.

str = {...
'State Time: 12:00:00.123 (43200.123)'
'State Time: 12:00:01.456 (43200.456)'
'State Time: 12:00:02.789 (43200.789)'};
str = sprintf('%s\n', str{:});
tok = regexp(str, 'State Time:\s+(\d+:\d+:[\d\.]+)\s+\(([\d\.]+)\)', 'tokens');
tok = cat(1, tok{:});

1 Comment


Brad (view profile)

on 19 Jun 2013

Kelly, thanks for the heads up on matching the parentheses. Will definitely be using this in the future as well!!

Kelly Kearney

Kelly Kearney (view profile)

Contact us