Setting XTicks with Datetime reuslt in an error
Show older comments
Hi Together
normaly I can solve all my issues with a search here, but for this problem i didn't found any workins solution:
I have Data, which correlates with a date.
now I want to set the x axis correlating to the data, but anything i try, result in the same error:
this one does not work:
xtickformat('yyyy-MM-dd')
xticks(datetime(conf_t(day_sel):hours(2):datetime(conf_t(day_sel))+hours(24)))
another try:
ticlocs = datetime(conf_t(day_sel):hours(2):datetime(conf_t(day_sel))+hours(24)); %datetime(DateString); %R2014b or later
ticlocs.Format = 'dd-MMM-yyyy';
set(gca, 'XTick', ticlocs);
axes('XTick', datetime(conf_t(day_sel):hours(2):datetime(conf_t(day_sel))+hours(24)) , 'XTickLabelMode', 'manual')
Error using set
Value must be a vector of type single or double whose values increase.
axes('XTick', datetime(conf_t(day_sel):hours(2):datetime(conf_t(day_sel))+hours(24)) , 'XTickLabelMode', 'manual')
Error using axes
Value must be a vector of type single or double whose values increase.
direct trial:
xticks(datetime(conf_t(day_sel):hours(2):datetime(conf_t(day_sel))+hours(24)))
Error using xticks
Tick values must be a vector of increasing numeric values.
BUT: my date has increasing Values:
datetime(conf_t(day_sel)):hours(2):datetime(conf_t(day_sel))+hours(24))
ans =
1×13 datetime array
Columns 1 through 7
31-Jul-2022 14:00:00 31-Jul-2022 16:00:00 31-Jul-2022 18:00:00 31-Jul-2022 20:00:00 31-Jul-2022 22:00:00 01-Aug-2022 00:00:00 01-Aug-2022 02:00:00
Columns 8 through 13
01-Aug-2022 04:00:00 01-Aug-2022 06:00:00 01-Aug-2022 08:00:00 01-Aug-2022 10:00:00 01-Aug-2022 12:00:00 01-Aug-2022 14:00:00
so
how can I create an Axis with an 2h interval with my required date?
thanks in advance!!
Accepted Answer
More Answers (1)
you can use datetimes directly as ticks. To make sure that all ticks are shown you can use the xticks command, and for readability i would recommend to rotate them a bit using xtickangle
MyTimes = ["31-Jul-2022 14:00:00"; "31-Jul-2022 16:00:00"; "31-Jul-2022 18:00:00"; "31-Jul-2022 20:00:00"; "31-Jul-2022 22:00:00"; "01-Aug-2022 00:00:00"; "01-Aug-2022 02:00:00"; "01-Aug-2022 04:00:00"; "01-Aug-2022 06:00:00"; "01-Aug-2022 08:00:00"; "01-Aug-2022 10:00:00"; "01-Aug-2022 12:00:00"; "01-Aug-2022 14:00:00"];
MyTimes = datetime(MyTimes);
figure
stem(MyTimes,rand(1,numel(MyTimes)))
xticks(MyTimes) % add this step to make sure that all ticks are shown
xtickangle(45) % add this to rotate the ticks for sake of readability
Categories
Find more on Linear Predictive Coding 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!