How to set dates for x-axis?

2 ビュー (過去 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 Center および File ExchangeGrid 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