rounding and displaying datetime in plot x-axis

조회 수: 9 (최근 30일)
Joel Affolter
Joel Affolter 2020년 11월 26일
댓글: Joel Affolter 2020년 12월 3일
I want to display the energy versus the time of up to 15 measurements. Currently I can either get a satisfying number and position of ticks but with an annoying date stamp (option 1) or can remove the date stamp but get a weird rounding error. I would like to get rid of the date stamp while keeping the options to have "round" values of my datetime.
Figure 1 is option 1 and figure 2 is option 2.
option 1 option 2
You find a sample code further beneath (I adjusted it so you can run it yourself, in my case the data is extracted from a text file and the datetime is not always the same vector, but all measurements are done within a day and to not go over midnight), to switch from option 1 to 2 simply uncomment BOTH lines with
%ax = gca;
%ax.XTickLabel = datestr(linspace(xtick_start,xtick_end,NumTicks),'HH:mm');
I hope the solution is straightforwar and I was just too stupid to realize my mistake... Many thanks in advance either way
n_experiments = 5;
n_length = 10;
%generate random data (not done in my application)
for i_n_experiments = 1:n_experiments
t1 = datetime(2013,11,1,8,0,0);
t2 = datetime(2013,11,1,15,30,0);
test_data(i_n_experiments).datetime = linspace(t1,t2,n_length);
test_data(i_n_experiments).E = rand(n_length,1);
end
%define some quantities related to the size of the structure array
[~,n_series] = size(test_data);
n_series_vec = 1:n_series;
ax_vec = zeros(1,n_series);
t = tiledlayout('flow');
for i = 1:n_series
ax_vec(i) = nexttile;
plot(test_data(i).datetime, test_data(i).E, 'Linewidth', 4)
% THIS IS THE PART I AM CONCERNED WITH
NumTicks = 5; %define number of ticks displayed
xtick_start = dateshift(test_data(i).datetime(1), 'end', 'hour'); %round the datetime, so labels of adjacent graphs won't overlap
xtick_end = dateshift(test_data(i).datetime(end), 'start', 'hour');
%ax = gca;
%ax.XTickLabel = datestr(linspace(xtick_start,xtick_end,NumTicks),'HH:mm');
set(gca,'XTick',linspace(xtick_start,xtick_end,NumTicks))
end
%synchronize y axis
linkaxes([ax_vec(1:n_series)],'y')

채택된 답변

Divija Aleti
Divija Aleti 2020년 11월 30일
Hi Joel,
In the code for 'option 2' which you have given above, replace
ax.XTickLabel = datestr(linspace(xtick_start,xtick_end,NumTicks),'HH:mm');
with
ax.XTickLabel = datestr(linspace(xtick_start,xtick_end,NumTicks),'HH:MM');
to get the desired output.
This is because 'mm' (small letters) indicates the 'month' in two digits while 'MM' (capital letters) indicates the 'minute' in two digits.
Have a look at the following link for additional information:
  댓글 수: 1
Joel Affolter
Joel Affolter 2020년 12월 3일
This makes perfect sense, what a stupid mistake... Thanks a lot!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by