time formatting in matlab

254 views Asked by At

I have time series per minute that look as shown below. The format is like '"hh:mm"' as seen below

'"8:59"' '"9:00"' '"9:01"' '"9:02"' '"9:03"' '"9:04"' '"9:05"' '"9:06"' '"9:07"'

Is it possible to change these to hhmm format in matlab like 859 900 901 902 t0 a double format.

2

There are 2 answers

2
sco1 On BEST ANSWER

You can use a function like strrep or regexprep to get rid of the colon in the string. For example:

t = {'"8:59"', '"9:00"', '"9:01"', '"9:02"'};
newt = strrep(t, ':', '');
newt = strrep(newt, '"', '');
newt = strrep(newt, ''', '');

Or the slightly bizarre regexprep call:

newt = regexprep(t, '[''''"'':]', '');

Returns

newt = 

    '859'    '900'    '901'    '902'

Which you can then use with str2double to generate your vector of doubles:

tdouble = str2double(newt);

Which returns:

tdouble =

   859   900   901   902

I'd also recommend taking a look at datenum and datestr, which are the MATLAB builtins for manipulating serial date values. For example:

tother = datenum(t, 'HH:MM');

Returns:

tother =

   1.0e+05 *

    7.3597
    7.3597
    7.3597
    7.3597

And to go back:

totherstr = datestr(tother, 'HH:MM');

Which returns:

totherstr =

08:59
09:00
09:01
09:02
0
rst On

To string

 strjoin(strsplit(s, ':'),'')

to double

 str2num(strjoin(strsplit(s, ':'),''))

If you have those quotes "" and want to remove them, then use this here

 str2num(strjoin(strsplit(s(3:end-2), ':'),''))