Setting XTicks with Datetime reuslt in an error

조회 수: 2(최근 30일)
Wolfram Murr
Wolfram Murr 2022년 8월 26일
편집: Rik 2022년 8월 28일
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:
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:
Error using xticks
Tick values must be a vector of increasing numeric values.
BUT: my date has increasing Values:
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
how can I create an Axis with an 2h interval with my required date?
thanks in advance!!

채택된 답변

Wolfram Murr
Wolfram Murr 2022년 8월 26일
편집: Rik 2022년 8월 28일
Hi Karim,
thanks for your reply, unfortunatelly i do generate the dates over several days:
conf_t = {'27-Jul-2022 14:00:00';'28-Jul-2022 14:00:00';'29-Jul-2022 14:00:00';'30-Jul-2022 14:00:00';'31-Jul-2022 14:00:00'};%
Seq2 = [2 2 4 6 8 10 12 14 16 18 20 4; 2 2 2 2 2 2 2 2 2 2 2 2]; %Sequenz 2 mit den verschiedenen Flüssen + Zeiten in h
start_utc = ((datenum(conf_t) - datenum('01-Jan-1970'))*86400)-7200;
S_utc(:,1) = start_utc(:); % startwert
for k = 1:length(S_utc(:,1))
for i = 1:length(Seq2(1,:)+1)% Utc zeiten gem S1/S2 erzeugen
S_utc(k,i+1) = S_utc(k,i)+Seq2(2,i)*3600;
end % Utc Zeiten erzeugen
which makes it not practicable to write this manualy
your reply brought me to an Idea, an research and the solution
the different kind of brackets/ Type of Data was the problem.
here is the Solution (which works)
xticks(reshape(datetime(conf_t(day_sel):hours(2):datetime(conf_t(day_sel))+hours(24)), 1, []))
(just add the "reshape" function)

추가 답변(1개)

Karim 2022년 8월 26일
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);
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

Community Treasure Hunt

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

Start Hunting!

Translated by