How to set dates for x-axis?

13 views (last 30 days)
MC3105
MC3105 on 9 Nov 2014
Answered: Star Strider on 9 Nov 2014
Hey everyone,
I know this has been discussed on the internet numerously, but I still cannot find a solution to my problem:
I am using a date vector that contains dates and times from
start=datenum(2010,05,15,0,0,0);
to
fin=datenum(2010,05,30,23,0,0);
So each day contains 24 values (for the 24 hours of the day).
Now I want to plot some data for these dates and I want a tick for each day on my x-axis and a label for each tick like this: May 15, May 16, May 17 .... May 30. The tick should be in the middle of each day, so every day at 12 o'clock would be great. How can I do that?
Thanks a lot!!

Accepted Answer

Star Strider
Star Strider on 9 Nov 2014
This works:
start = datenum(2010,05,15,0,0,0);
fin=datenum(2010,05,30,23,0,0);
hrvct = start:(1/24):fin; % Create Hourly Dates
dtks = start+0.5:fin; % Ticks At Noon
dhv = datevec(dtks);
y = (0:length(hrvct)-1)*0.005-sin((0:length(hrvct)-1)/24*2*pi+0.25)*5+randn(size(hrvct))*0.05+20; % Create Y-Data
figure(1)
plot(hrvct, y)
grid
set(gca, 'XTick', dtks, 'XTickLabelRotation',30) % Label Rotation (R2014b)
datetick('x', 'mmm dd', 'keepticks')
xlabel('Time')
ylabel('Temperature (\circC)')
You have to have R2014b to use the 'XTickLabelRotation' property. Otherwise, either reduce your x-axis font size or use the text function to create a vector of rotated dates.
Example plot:

More Answers (0)

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!