Plotting 'HH:MM' format times against the X axis

10 views (last 30 days)
I have a string of 'HH:MM' times that are based off of a set of decimal times. I really want to make a plot that displays my 'HH:MM' times though.
% taking my times (zero to 23:50 in ten minute incriments) and converting
% to decimal then 'HH:MM' format
decimalTimes = zeros(length(times),1);
for k = 1:length(times)
hhTimes = floor(times(k)/100);
mmTimes = rem(times(k), 100)/60;
decimalTimes(k) = hhTimes + mmTimes;
end
dateTimes = datestr(decimalTimes/24, 'HH:MM');
plot(decimalTimes,speeds,'s-r','LineWidth',lw);
% plot formatting
hold on
legend('10 minute average wind speeds for Dec. 24, 2011', 'Location', 'NorthEast','FontSize',legv);
title("Average Wind Speeds every 10 Minutes for One Full Day")
xlabel("Time (10 minute incriments)")
ylabel("Average Wind Speed")
hold off
Now from here absoluteley nothing I am trying is working to get the x axis to display times in the 'HH:MM' format. Please help.

Answers (3)

Voss
Voss on 22 Sep 2023
Edited: Voss on 22 Sep 2023
times = (0:24*6-1)*10
times = 1×144
0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290
speeds = rand(size(times));
lw = 2;
legv = 11;
dateTimes = datetime(2011,12,24,0,times,0, 'Format','HH:mm')
dateTimes = 1×144 datetime array
Columns 1 through 24 00:00 00:10 00:20 00:30 00:40 00:50 01:00 01:10 01:20 01:30 01:40 01:50 02:00 02:10 02:20 02:30 02:40 02:50 03:00 03:10 03:20 03:30 03:40 03:50 Columns 25 through 48 04:00 04:10 04:20 04:30 04:40 04:50 05:00 05:10 05:20 05:30 05:40 05:50 06:00 06:10 06:20 06:30 06:40 06:50 07:00 07:10 07:20 07:30 07:40 07:50 Columns 49 through 72 08:00 08:10 08:20 08:30 08:40 08:50 09:00 09:10 09:20 09:30 09:40 09:50 10:00 10:10 10:20 10:30 10:40 10:50 11:00 11:10 11:20 11:30 11:40 11:50 Columns 73 through 96 12:00 12:10 12:20 12:30 12:40 12:50 13:00 13:10 13:20 13:30 13:40 13:50 14:00 14:10 14:20 14:30 14:40 14:50 15:00 15:10 15:20 15:30 15:40 15:50 Columns 97 through 120 16:00 16:10 16:20 16:30 16:40 16:50 17:00 17:10 17:20 17:30 17:40 17:50 18:00 18:10 18:20 18:30 18:40 18:50 19:00 19:10 19:20 19:30 19:40 19:50 Columns 121 through 144 20:00 20:10 20:20 20:30 20:40 20:50 21:00 21:10 21:20 21:30 21:40 21:50 22:00 22:10 22:20 22:30 22:40 22:50 23:00 23:10 23:20 23:30 23:40 23:50
plot(dateTimes,speeds,'s-r','LineWidth',lw);
% plot formatting
hold on
legend('10 minute average wind speeds for Dec. 24, 2011', 'Location', 'NorthEast','FontSize',legv);
title("Average Wind Speeds every 10 Minutes for One Full Day")
xlabel("Time (10 minute increments)")
ylabel("Average Wind Speed")
hold off

Star Strider
Star Strider on 22 Sep 2023
I am not certain what ‘decimal times’ are, however it appears that they might be something like ‘HHMM’ where all are four-digit integers.
If so, try this —
times = [1100; 1115; 1130; 1145; 1200; 1215; 1230; 1245; 1300]
times = 9×1
1100 1115 1130 1145 1200 1215 1230 1245 1300
dateTimes = datetime(string(times), 'InputFormat','HHmm', 'Format','HH:mm')
dateTimes = 9×1 datetime array
11:00 11:15 11:30 11:45 12:00 12:15 12:30 12:45 13:00
y = sin((0:numel(dateTimes)-1)*2*pi/(numel(dateTimes)-1));
figure
plot(dateTimes, y)
grid
xsecondarylabel("");
.

Stephen23
Stephen23 on 23 Sep 2023
Edited: Stephen23 on 23 Sep 2023
"taking my times (zero to 23:50 in ten minute incriments) and converting to decimal then 'HH:MM' format"
Do not convert to "decimal".
Do not use deprecated DATESTR or DATENUM or DATEVEC.
Do not use inappropriate DATETIME.
Plot using the DURATION type, it is the correct tool for the job:
% fake data
dur = minutes(0):minutes(10):minutes(24*60-1);
dur.Format = 'hh:mm';
vec = rand(1,numel(dur));
% plot
plot(dur,vec)
To adjust how many tickmarks there are use the tickmark controls e.g. XTICKS.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!