I want to plot all the day numbers on x-axis. I have really big timeseries data with 10-min wind speed averages, starting from 1st April and ending 30th April. I want to plot the wind speed, measured every 10min during the day, for the whole month. And on the x-axis I want to have ticks for every day starting from 1 till 30. Here is the code:
%% direct timetable importing
clc
clear
load april.mat %april timetable
dnum = day(ex1.timestamp);
mnum = month(ex1.timestamp);
myear = year(ex1.timestamp);
myear = unique(myear); %finding year
mnum = unique(mnum); %fingind month number
dlast = unique(max(dnum)); %finding last day
duniq = unique(dnum);
duniq = num2cell(duniq);
plot(ex1.timestamp,ex1.CH1Avg)
hAx = gca;
xlim(datetime([myear myear],[mnum mnum],[1 dlast])); %setting limits
xticklabels(duniq); %setting ticks to 30 like the number of days in April
datetick('x','dd'); %using date ticks, not working when trying dd
When i use in datetick('x','d') it is working. But when i use datetick('x' ,'dd') -it is not working. I also tried with datetick('x','d','keepticks'), but not working properly.
Can someone please help me?
Like it is shown on screenshot I want on x axis to have evenly distributed ticks for every day, starting from 01,02... ending at 30. like it is set in xlim

 採用された回答

Star Strider
Star Strider 2021 年 4 月 18 日

0 投票

It is necessary to be creative with the plot and the tick labels.
Try this:
DL = load('april.mat');
ex1 = DL.ex1;
Q1 = ex1(1:10,:);
monthinfo = ex1.timestamp(1);
daylim = eomday(year(monthinfo),month(monthinfo));
figure
plot((1:size(ex1,1)),ex1.CH1Avg)
xt = get(gca,'XTick');
set(gca,'XTick',linspace(min(xt),max(xt),daylim), 'XTickLabel',compose('%02d',(1:daylim)))
Experiment to get the result you want.

4 件のコメント

Gligor Terziev
Gligor Terziev 2021 年 4 月 18 日
This is how I fixed.
clc
clear
load april.mat %creates timetable directly
start1 = '01.04.2011 21:00:00';
end1 = '30.04.2011 23:59:00';
startdate = datetime(start1,'InputFormat','dd.MM.yyyy HH:mm:ss');
enddate = datetime(end1,'InputFormat','dd.MM.yyyy HH:mm:ss');
getrows = isbetween(ex1.timestamp, startdate, enddate);
dnum = day(ex1.timestamp);
mnum = month(ex1.timestamp);
myear = year(ex1.timestamp);
myear = unique(myear); %finding year
mnum = unique(mnum); %fingind month number
dlast = unique(max(dnum)); %finding last day
% finding avg wind speed
[gNum1,gName1] = findgroups(dnum);
avgWS = splitapply(@mean,ex1.CH1Avg,gNum1); %finding average wind speed per day
avg_last= mean(avgWS); %finding average wind speed for month
%%plotting
figure
plot(ex1.timestamp(getrows),ex1.CH1Avg(getrows))
grid
hAx = gca;
xlim([datetime(startdate) datetime(enddate)])
xtickformat('d')
xticks(datetime(startdate) : caldays(1) : datetime(enddate))
Btw when I run your code, somehow the last day it is not plotted. Look at the screenshot. With my code it is working perfectly. Btw thanks a lot.
Thanks a lot for your fast response @Star Strider
Star Strider
Star Strider 2021 年 4 月 18 日
Try this:
DL = load('april.mat');
ex1 = DL.ex1;
monthinfo = ex1.timestamp([1 end]);
daylim = day(monthinfo);
figure
plot((1:size(ex1,1)),ex1.CH1Avg)
xt = get(gca,'XTick');
xlim([0, size(ex1,1)])
set(gca,'XTick',linspace(min(xt),size(ex1,1),daylim(2)), 'XTickLabel',compose('%02d',(daylim(1):daylim(2))))
That should plot the x-ticks and x-limits correctly.
Gligor Terziev
Gligor Terziev 2021 年 4 月 19 日
Thanks a lot.
Star Strider
Star Strider 2021 年 4 月 19 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by