Change xaxis from numbers to dates

I have plotted values over a year, and i would like to change the xaxis from numbers to dates.
This is just an example, but this code should be somewhat the same as what i get when i plot.
x = [1:1:8760];
y = x+5;
figure()
plot(x,y)
So for this year, which is energy values for 2019, i would like the xaxis to only have a value for January 2019 June 2019 and December 2019 as what appears at the xaxis.
I have no idea how to do this, and any help would be very helpfull!

 Accepted Answer

x = 1:8760;
y = x+5;
figure()
% plot(x,y)
dt = datetime(2019,1,1,x-1,0,0);
dt([1 end])
ans = 1×2 datetime array
01-Jan-2019 00:00:00 31-Dec-2019 23:00:00
plot(dt,y)
set(gca(),'XTick',datetime(2019,[1 6 12],1))

More Answers (0)

Products

Release

R2020b

Tags

Asked:

on 8 May 2022

Answered:

on 8 May 2022

Community Treasure Hunt

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

Start Hunting!