datetime matlab different temporal resolution

霸气de小男生 提交于 2019-12-13 16:25:33

问题


I am trying to plot two timeseries in one graph. Unfortunatelly, the data sets have different temporal resolutions and my code using datetime does not work. My aim is one xtick per hour. Any idea how I can solve that problem? Thanks!

dataset1 = rand(1,230).';
dataset2 = rand(1,33).';

xstart = datenum('19/02 09:00','dd/mm HH:MM');
xend = datenum('21/02 18:00','dd/mm HH:MM');
x = linspace(xstart,xend,20);
Dat = linspace(xstart,xend,numel(dataset1));
x1=[1:1:230].' %values every 15 minutes


x0_OM = datenum('19/02 09:00','dd/mm HH:MM');
x1_OM = datenum('20/02 18:00','dd/mm HH:MM');
xData = linspace(x0_OM,x1_OM,20);
Dat2 = linspace(xstart,xend,numel(dataset2));
x2=[1:4:130].' %hourly values


fig=figure ();

yyaxis left
plot(x1,dataset1);
ylabel('Dataset 1')
xlabel('timesteps (15min Interval)');
yyaxis right
plot(x2,dataset2);
ylabel('Dataset 2')
set(gca,'XTick', xData) %does not work
datetick('x', 'dd/mm HH:MM', 'keeplimits','keepticks') %does not work

回答1:


I generalized your code a bit and used something nicer to inspect than random numbers. I removed the labelling part to keep the script short.

% Dataset 1, 15 minutes interval
xstart1 = datenum('19/02 09:00','dd/mm HH:MM');
xend1 = datenum('21/02 18:00','dd/mm HH:MM');
Dat1 = xstart1:1/24/4:xend1;                           % 1/24/4 is a 15 minutes step
dataset1 = sin(linspace(0, 2*pi, numel(Dat1)));

% Dataset 2, 1 hour interval
xstart2 = datenum('19/02 09:00', 'dd/mm HH:MM');
xend2 = datenum('20/02 18:00', 'dd/mm HH:MM');
Dat2 = xstart2:1/24:xend2;                             % 1/24 is a 1 hour step 
dataset2 = cos(linspace(0, 2*pi, numel(Dat2)));

% Determine "global" start and end.
xstart = min(xstart1, xstart2);
xend = max(xend1, xend2);
Dat = xstart:1/24:xend;

% Plot
fig = figure();
hold on;
plot(Dat1, dataset1, '*');
plot(Dat2, dataset2, 'r*');
set(gca, 'XTick', Dat);
datetick('x', 'dd/mm HH:MM', 'keepticks', 'keeplimits');
hold off;

Principally, that should work, but the output is not nice, due to the long tick labels. Could you please check, if that is, what you wanted to achieve?




回答2:


The last two commands actually work, but unfortunaltely the ticks are just at another place than the graph is. Your x1 (and x2) values are from 1 to 230 while the xData values for the ticks are around 730000. If you choose the x values for the plot at the datenum values it works.
Another issue is that the length of the vectors dont add up to values for every 15 minutes (or 1 hour). If you want values for every 15 minutes for the timespan from 19/02 09:00 to 21/02 18:00 (57 hours in total) you need:

4(1/h)*57(hours) + 1 for the last value = 229 values

or in general:

(timespan / timewindow) + 1

If you apply those changes to your code you get

dataset1 = rand(1,229).';
dataset2 = rand(1,34).';

xstart = datenum('19/02 09:00','dd/mm HH:MM');
xend = datenum('21/02 18:00','dd/mm HH:MM');

% in datenumformat 1 = 24 hours
fifteenminutes=(1/24/4);%15 minutes
spacing_in_15min=((xend-xstart)/fifteenminutes)+1;%duration devided by timewindow, +1 for last value
x1 = linspace(xstart,xend,spacing_in_15min); %values every 15 minutes

x0_OM = datenum('19/02 09:00','dd/mm HH:MM');
x1_OM = datenum('20/02 18:00','dd/mm HH:MM');
onehour=1/24; %one hoour
spacing_in_1hour=((x1_OM-x0_OM)/onehour)+1;%duration devided by timewindow, +1 for last value
x2 = linspace(x0_OM,x1_OM,spacing_in_1hour); %hourly values
tickvalues = linspace(xstart,xend,((xend-xstart)/onehour)+1);

fig=figure ();

yyaxis left
plot(x1,dataset1);
ylabel('Dataset 1')
xlabel('timesteps (15min Interval)');
yyaxis right
plot(x2,dataset2);
ylabel('Dataset 2')
set(gca(1),'XTick', tickvalues); %Ticks every hour for the larger dataset
set(gca(1),'XLim', [x2(1) x2(end)]); %focus on the time with both datasets
datetick('x', 'dd/mm HH:MM', 'keeplimits','keepticks'); %Tickformat

Which I think is what you were looking for. I have removed a few values (x,Dat,xData) which were unused. Unfortunaltey even in full screen mode 34 Tick values is a lot so you might want to change the Tickformat or zoom in on a special part. If you have to do more works in this area I recommend you to look into the MATLAB datetime format, that I find a bit better to handle than the datenum.



来源:https://stackoverflow.com/questions/55017690/datetime-matlab-different-temporal-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!