transforming minutes into time

1 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2016-5-6
编辑:Star Strider 2016-5-6
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 个评论
Star Strider
Star Strider 2016-5-6
‘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')}

请先登录,再进行评论。

更多回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-5-6
编辑:Azzi Abdelmalek 2016-5-6
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')

AA
AA 2016-5-6
thanks but I need it as cell array with 05:10 not 5:10. I have to transform the charr to cell array. how do i do that

类别

Help CenterFile Exchange 中查找有关 Time Series 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by