Plotting 'HH:MM' format times against the X axis
10 views (last 30 days)
Show older comments
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.
0 Comments
Answers (3)
Voss
on 22 Sep 2023
Edited: Voss
on 22 Sep 2023
times = (0:24*6-1)*10
speeds = rand(size(times));
lw = 2;
legv = 11;
dateTimes = datetime(2011,12,24,0,times,0, 'Format','HH:mm')
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
0 Comments
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]
dateTimes = datetime(string(times), 'InputFormat','HHmm', 'Format','HH:mm')
y = sin((0:numel(dateTimes)-1)*2*pi/(numel(dateTimes)-1));
figure
plot(dateTimes, y)
grid
xsecondarylabel("");
.
0 Comments
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.
0 Comments
See Also
Categories
Find more on Axis Labels 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!