transforming minutes into time
1 view (last 30 days)
Show older comments
let col1 contain the following values
905
990
1117
1228
1306
16
149
211
276
347
418
478
533
581
631
679
730
795
How can I transform the above values into time HH:MM. 905/60=15,08. or 15:05 o'clock and so on
0 Comments
Accepted Answer
Star Strider
on 6 May 2016
Edited: Star Strider
on 6 May 2016
Your minutes array is rolling over to a new day at index = 5, so this adds days whenever the day ‘resets’.
Version #1 — if you have repelem (new in R2015a):
T2400 = [1; find(diff(Tv) < 0); size(Tv,1)+1];
Tv = Tv + repelem([0:length(diff(T2400))-1], diff(T2400))' * 1440;
Result = datestr(datenum([01 01 01]) + Tv/1440, 'HH:MM')
Version #2 (without repelem):
T2400 = [1; find(diff(Tv) < 0); size(Tv,1)];
days = 0;
for k1 = 1:length(T2400)-1
Tv(T2400(k1):T2400(k1+1)) = Tv(T2400(k1):T2400(k1+1)) + days*1440;
days = days + 1;
end
Result = datestr(datenum([01 01 01]) + Tv/1440, 'HH:MM')
Result =
15:05
16:30
18:37
20:28
21:46
00:16
02:29
03:31
04:36
05:47
06:58
07:58
08:53
09:41
10:31
11:19
12:10
13:15
EDIT — Added ‘Version #1’.
1 Comment
Star Strider
on 6 May 2016
‘thanks but I need it as cell array with 05:10 not 5:10.’
My code give the leading zeros, as my previous post demonstrates.
What do you want?
‘I have to transform the charr to cell array. how do i do that’
One easy way is to put curly braces ‘{}’ around the right-hand side:
Result = {datestr(datenum([01 01 01]) + Tv/1440, 'HH:MM')}
More Answers (2)
Azzi Abdelmalek
on 6 May 2016
Edited: Azzi Abdelmalek
on 6 May 2016
a=[90;990;;1117;54;60]
h=fix(a/60)
m=mod(a,60)
out=arrayfun(@(x,y) [sprintf('%d',x) ':' sprintf('%d',y)],h,m,'un',0)
k=datenum(out,'HH:MM')
out=datestr(k,'HH:MM')
0 Comments
See Also
Categories
Find more on Numeric Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!