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

 Akzeptierte Antwort

Chunru
Chunru am 23 Jun. 2021

1 Stimme

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 Kommentare

AA
AA am 23 Jun. 2021
Thanks for your responses:
Here is a bit modification:
how can we change if there are three set of data;
%%here time is -100 to 100 with interval of 0.2 sec
%day is the number of segments (12 hours)
%z2 is respective data for each points
imagesc(time,day,z2) %here time is -100 to 100 with interval of 0.2 sec
colormap(jet)
xlim([-100 100]);
NumTicks = 4;
L = get(gca,'YLim');
set(gca,'YTick',linspace(L(1),L(2),NumTicks));
datetick('y','mmmdd','keeplimits','keepticks');
ylabel('(Date)');
In this case, is it possible to convert the day (no of segments) to date?
Chunru
Chunru am 23 Jun. 2021
Bearbeitet: Chunru am 23 Jun. 2021
For imagesc, you cannot use datetime as axis variable. You need to change axis labels instead.
t = [-100:.2:100];
d = [0:10];
imagesc(t,d,rand(20,20));
%YTickStr = string(datetime('10/10/2018', 'InputFormat', 'dd/MM/yyyy', 'Format', 'dd/MM/yyyy') + hours(d*12));
YTickStr = char(datetime('10/10/2018', 'InputFormat', 'dd/MM/yyyy', 'Format', 'dd/MM/yyyy') + hours(d*12));
set(gca, 'XTick', -100:50:100, 'XTickLabel', char((-100:50:100)'*.2+"s"), ...
'YTick', 0:10, 'YTickLabel', YTickStr);
AA
AA am 23 Jun. 2021
Bearbeitet: AA am 23 Jun. 2021
Thanks for sharing a detialed answer,
I got an error "Undefined function 'string' for input arguments of type 'datetime'."
*I am using Matlab 2016a
Here is the modified code,
data information:
day 1*1420 double
time 1*3001 double (this case has -300:300)
z2 1420*3001 double
fig=figure(ii);
h1=subplot(3,1,1);
imagesc(time,day,z2);
colormap(jet)
YTickStr = string(datetime('10/10/2018', 'InputFormat', 'dd/MM/yyyy', 'Format', 'dd/MM/yyyy') + hours(day*12));
set(gca, 'XTick', -300:50:300, 'XTickLabel','YTick', 1:lastday, 'YTickLabel', YTickStr);
Chunru
Chunru am 23 Jun. 2021
Then change string to char.
AA
AA am 23 Jun. 2021
Thanks boss :) but
Error using matlab.graphics.axis.Axes/set
Invalid parameter/value pair arguments.
Error in plot_CF (line 55)
set(g
ca, 'XTick', -300:50:300, 'XTickLabel','YTick', 1:lastday, 'YTickLabel', YTickStr);
Chunru
Chunru am 23 Jun. 2021
You miss simething after 'XTickLabel'.
AA
AA am 23 Jun. 2021
Bearbeitet: AA am 23 Jun. 2021
thank you very much, it works.
AA
AA am 23 Jun. 2021
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

KSSV
KSSV am 23 Jun. 2021

1 Stimme

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 am 23 Jun. 2021

1 Stimme

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')

Kategorien

Gefragt:

AA
am 23 Jun. 2021

Kommentiert:

AA
am 23 Jun. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by