Setting XTicks with Datetime reuslt in an error

조회 수: 33 (최근 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:
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!!

채택된 답변

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
end % Utc Zeiten erzeugen
which makes it not practicable to write this manualy
BUT
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
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);
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

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by