How to set dates for x-axis?

조회 수: 3 (최근 30일)
MC3105
MC3105 2014년 11월 9일
답변: Star Strider 2014년 11월 9일
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!!

채택된 답변

Star Strider
Star Strider 2014년 11월 9일
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:

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by