rounding and displaying datetime in plot x-axis
    7 次查看(过去 30 天)
  
       显示 更早的评论
    
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.
  
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')  
0 个评论
采纳的回答
  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:
更多回答(0 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!