rounding and displaying datetime in plot x-axis

6 ビュー (過去 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 Center および File ExchangeGeographic Plots についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by