MATLAB: Time series, resample, axes, datetime

axesdatetimeresampletime series

Anyone to lead me in the right direction please?
I have energy consumption data of more than 3 years for every two seconds (43200 per day). Different households. How do I set the x axis into 24hrs (00:00 – 23:59) and plot each day data on one graph?
I have two variables (power and time). Files are saved as dates and have 43200 data each for power and time. I also have two variables called start_date and end_date. I would like to select any range of dates and get those dates data plotted in one graph. Later I will need to resample the time since, color each day plot, etc. I have attached a figure to explain more/less what im trying to do.
I highly appreciate your assistance in this. Thanks

Best Answer

Use date numbers...example for an axes...
>> dn=datenum(2000,1,1,0,0,[0:2:86400-1].'); % a day at 2-sec interval
>> t=dn-dn(1); % make 0-based
>> p=normpdf(t,0.5,.1); % some data to plot...
>> plot(t,p)
>> set(gca,'xtick',[0:2:24]/24) % set tick marks at 2-hr
>> datetick('x','HH:MM','keeplimits','keepticks') % format the time axis
>> set(gca,'xminortick','on') % minor ticks for odd hrs
>>
For the selection, convert the requested time ranges into date numbers, too, and use the logical operations to select between, inclusive or not, etc., ...
NB: When generating series of date numbers, always use integer multiples of the smaller time granule such as seconds above rather than dividing out the fraction of an hour or day and using floating point deltas. The latter will end up with floating point comparisons that do NOT compare owing to that rounding whereas the rounding will be consistent inside datenum and friends if use the integer deltas. As can be seen in the above example, the functions are "smart enough" to know how to wrap times when building the vector.
In your case, you'll be reading a time vector, presumably as string; simply convert it or ignore it and just build the time history as shown.
The example does not use the newer timeseries class; I don't have it in the version installed here. If choose to use it, some of the machinations are a little different but the ideas are the same. One thing you do have to be aware of is that there's a whole different set of formatting abbreviations between the two.