MATLAB Answers

Plotting date (number) on x-axis

30 views (last 30 days)
Gligor Terziev
Gligor Terziev on 18 Apr 2021 at 13:16
Commented: Star Strider on 19 Apr 2021 at 14:42
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

Accepted Answer

Star Strider
Star Strider on 18 Apr 2021 at 21:12
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 Comments
Star Strider
Star Strider on 19 Apr 2021 at 14:42
As always, my pleasure!

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!