How do I plot military time?

9 views (last 30 days)
Thanh Nguyen
Thanh Nguyen on 25 Sep 2018
Commented: Peter Perkins on 1 Oct 2018
So I'm working on a mini project that involves using an Excel sheet. I want to be able to plot temperature, altitude and time (time versus temp/alt in two graphs). I can be selective a choose the range for x and y values but I feel as if I'm cheating myself a bit. The issue here is that my time is calculated in seconds. The first value being: 86257 (representing the real-time just before midnight) and then the time continues into the next day at 2128.
Is there a way to plot this form of time in HH:MM:SS format from military form? My current code is listed below:
num = xlsread('Test.xlsx'); time = num(:,4); altitude = num(:,18); temperature = num(:,28);
figure(1) plot(time(2:138) ,altitude(2:138))
figure(2) plot(time(2:138),temperature(2:138))

Answers (1)

jonas
jonas on 25 Sep 2018
Edited: jonas on 25 Sep 2018
Yes, it's possible. How? Depends on if you have a date associated with those values. Let's say you do not have a date, just a bunch of time-stamps. This will give you the time of day (not accounting for daylight savings):
t_day=duration(mod(seconds(t_military),hours(24)),'format','hh:mm:ss')
If you have a date it's even easier.
t_day=datetime(MyDate)+seconds(t_military)
In fact, if you are not interested in displaying the date, then you might just choose an arbitrary date even if you do not have one.
After you plot, just specify the tickformat
xtickformat('HH:MM:SS')
  3 Comments
jonas
jonas on 25 Sep 2018
Edited: jonas on 25 Sep 2018
The solution above gives you HH:MM:SS format, but it is not steadily increasing. Instead it goes back to the origin (00:00) at midnight. I assume you want the curve to continue over to the next day?
This will probably do it for you
t=xlsread('Timestamp.xlsx')
d=[0;cumsum(diff(t)<0)]
t_day=datetime(1,1,1)+seconds(t)+days(d)
plot(t_day,t_day)
xtickformat('hh:mm:ss')
ytickformat('hh:mm:ss')
It's not the prettiest solution, but your data is quite troublesome as you do not have a date associated with those seconds.
Peter Perkins
Peter Perkins on 1 Oct 2018
As jonas is (I think) saying, if you want to plot against continuously increasing time and show only the 24-hour clockface time, you'll need to create a sequence of datetimes with the correct times of day, and then not show the date in the format of the tick labels.
If you want to overlay all days in the same 24-hour axis, then convert those datetimes to durations using the timeofday function.
If you want to to plot against continuously increasing time and not have the tick labels wrap back to zero at midnight (in other words continue on to 25:00:00 or whatever), convert the seconds to a duration and set the format to hh:mm:ss.

Sign in to comment.

Categories

Find more on Dates and Time 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!