Clear Filters
Clear Filters

Convert Time (string) to Time (numeric)

143 views (last 30 days)
Patrick Rungrugeecharoen
Patrick Rungrugeecharoen on 7 Jun 2019
Answered: Star Strider on 7 Jun 2019
Right now I have a vector string which is a 130611 x 1 string.
This string contains values such as:
The first row is 0 hrs 5 mins 0 secs, second row is 0 hrs 5 mins 0 secs, third row is 0 hrs 13 mins 0 secs etc.
I would like to return the same sized vector but telling me only minutes?
For instance "01:10:00" would return 70.

Answers (1)

Star Strider
Star Strider on 7 Jun 2019
You can use the datetime data type (with a few alterations, since duration for some reason does not accept datetime objects as arguments, or at least does not when I try it):
dtv = datevec(datetime({'00:05:00';'01:10:00';'23:59:59'},'InputFormat','HH:mm:ss'));
da = duration(dtv(:,4:end));
mins = minutes(da)
producing:
da =
00:05:00
01:10:00
23:59:59
mins =
5
70
1440
I posted ‘da’ to demonstrate the conversion. It is not necessary to return it specifically.
The duration data does not ‘wrap’ unless you also supply the year-month-day values, so as long as your data are all in the same 24-hour day, they will be accurate.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!