convert hours to days on y-axis

조회 수: 2 (최근 30일)
AA
AA 2021년 6월 23일
댓글: AA 2021년 6월 23일
Dear all,
I have a file a1, contains two columns, first one is number of segments (each segments represents 12 hours) for one years and seconds columns has data for that segment. I plot the data w.r.t hours.
Is it possible to convert scale from hours to day numbers or date?
here is the example of 6 day data, date started from 10 Oct 2018.
1 0
2 0
3 0
4 0.199999999999996
5 0
6 0
7 0
8 0.199999999999996
9 0
10 0
11 0
12 0
What i want to plot axis w.r.t date, here is the example
10/10/2018 0
0
11/10/2018 0
0.199999999999996
12/10/2018 0
0
Thanks in advance

채택된 답변

Chunru
Chunru 2021년 6월 23일
x = [
1 0
2 0
3 0
4 0.199999999999996
5 0
6 0
7 0
8 0.199999999999996
9 0
10 0
11 0
12 0];
bar(datetime('10/10/2018', 'InputFormat', 'dd/MM/yyyy') + hours(x(:,1)*12), x(:,2));
xtickformat('dd/MM/yyyy')
  댓글 수: 8
AA
AA 2021년 6월 23일
편집: AA 2021년 6월 23일
thank you very much, it works.
AA
AA 2021년 6월 23일
I found a problem with y-axis. if possible, please leave a comment
If i do not change the interval , it gives me so many values but later i changed the interval then it started from 1 JAN.
YTickStr:
10/10/18
10/10/18
10/10/18
11/10/18
11/10/18
11/10/18
11/10/18
12/10/18
12/10/18
12/10/18
12/10/18
13/10/18
13/10/18
Total segmetns lengths are : 1:1524
imagesc(time,day,z2);
colormap(jet)
YTickStr = char(datetime('10/10/2018', 'InputFormat', 'dd/MM/yy', 'Format', 'dd/MM/yy') + hours(day*6));
set(gca, 'YTick', 1:500:lengday, 'YTickLabel', YTickStr);
xlim([-100 100]);
here is output example for these format (without interval, 1:lengday)
with interval (1:500:lengday), it starts from 1 jan

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

KSSV
KSSV 2021년 6월 23일
A = [1 0
2 0
3 0
4 0.199999999999996
5 0
6 0
7 0
8 0.199999999999996
9 0
10 0
11 0
12 0];
t0 = datetime('10/10/2018') ;
[r,c] = size(A);
N = size(A,1)/2 ; % number of hours in a day
B = permute(reshape(A',[c,r/N,N]),[2,1,3]);
tn = t0+days(N) ;
t = (t0:tn)' ;
Each matrix in B corresponds to respective day in t.

Cris LaPierre
Cris LaPierre 2021년 6월 23일
If you convert your data to datetime, then you can set the y axis format to be any valid date format.
data = [1 0
2 0
3 0
4 0.199999999999996
5 0
6 0
7 0
8 0.199999999999996
9 0
10 0
11 0
12 0];
dataTT = array2timetable(data(:,2),'RowTimes',datetime(2018,10,10) + 12*hours(data(:,1)-1))
dataTT = 12×1 timetable
Time Var1 ____________________ ____ 10-Oct-2018 00:00:00 0 10-Oct-2018 12:00:00 0 11-Oct-2018 00:00:00 0 11-Oct-2018 12:00:00 0.2 12-Oct-2018 00:00:00 0 12-Oct-2018 12:00:00 0 13-Oct-2018 00:00:00 0 13-Oct-2018 12:00:00 0.2 14-Oct-2018 00:00:00 0 14-Oct-2018 12:00:00 0 15-Oct-2018 00:00:00 0 15-Oct-2018 12:00:00 0
plot(dataTT.Var1,dataTT.Time)
ytickformat('MM/dd/yyyy')

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by