hour to hour minute second conversion

13 次查看(过去 30 天)
I have an array represents hours, for example;
hour=[13.20,15.30,14.46,21];
Is there any matlab function to convert these hours to hour, minute, second? Example output;
hms=[13 12 0;15 18 0;14 27 36;21 0 0];

采纳的回答

Stephen23
Stephen23 2016-8-30
编辑:Stephen23 2016-8-30
The basic concept is easy to implement:
tmp = hour(:);
H = fix(tmp);
tmp = 60*(tmp-H);
M = fix(tmp);
tmp = 60*(tmp-M);
S = tmp;
hms = [H,M,S]
However this can lead to accumulated floating point errors. One way to resolve this would be to perform some intermediate rounding like this:
adj = 1e12;
tmp = round(hour(:)*adj)/adj;
H = fix(tmp);
tmp = round(60*(tmp-H)*adj)/adj;
M = fix(tmp);
tmp = round(60*(tmp-M)*adj)/adj;
S = tmp;
hms = [H,M,S]
to give
hms =
13 12 0
15 18 0
14 27 36
21 0 0

更多回答(1 个)

Teja Muppirala
Teja Muppirala 2016-8-30
编辑:Teja Muppirala 2016-8-30
myTimes=[13.20,15.30,14.46,21];
HR = hours(myTimes');
[H,M,S] = hms(HR);
HMS = [H M S]
This gives
HMS =
13 12 0
15 18 0
14 27 36
21 0 0

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by