MATLAB: Plotting specified date intervals

date plotdate x-axes

I have two date intervals: from
25-July-2017 from 9:00 AM to 25-July-2017 12:30 PM and from
26-July-2017 from 9:00 AM to 26-July-2017 12:30 PM.
I am not able to remove the duration in between (from 12:30 PM to the next day 9:00 AM).
A = readtable('Time and Sale_7_25_2017.xlsx');
B = readtable('Time and Sale_7_26_2017.xlsx');
A.Date = datetime(A.Date,'ConvertFrom','excel');
B.Date = datetime(B.Date,'ConvertFrom','excel');
figure(1)
plot(A.Date, A.Price,B.Date, B.Price, '-r', 'linewidth', 2);
datetick('x', 'dd-mmm HH:MM:SS','keeplimits','keepticks')
xlabel('Period')
ylabel('Price')
grid on
grid minor;

Best Answer

What I'm suggesting...
dn=datetime(['25-July-2017 9:00 AM'; '25-July-2017 12:30 PM']); % start/stop times day 1
dn=[dn(1):minutes(1):dn(2)].'; % datetime vector for day 1
p1=randn(size(dn)); p2=randn(size(dn)); % arbitrary data to go along for two days
A=table(dn,p1); % make a table similar to yours for demo purpose
dn=dn+1; B=table(dn,p2); % ditto, second day time vector (1 day later)
plot([A.p1;B.p2]) % plot the price data combined vs ordinal position
xlim([1 2*length(dn)]) % set the plot limits 1:N points total
hAx=gca; % get/save the axes handle
xt30=[1:30:211 212:30:422]; % indices of 30-minute intervals in ordinal vector
hAx.XTick=xt30; % set those tick marks to match
dntot=[A.dn;B.dn]; % the whole time vector concatenated
dastr=datestr(dnTot(hAx.XTick),'ddmmm HH:MM'); % convert to string represenation
dastr(8,:)=blanks(size(dastr,2)); % clear the point w/ almost overlaying ticks
hAx.XTickLabel=dastr; % write the tick labels as time strings
hAx.XTickLabelRotation=45; % rotate to give some room
grid on
NB: There are two ticks at the end of first day and beginning of second that are only a single point apart so there simply isn't enough room to label both. Hence the clearing of the last label in the first day to make room for the first in the second day.
You could make room by inserting some blank space but then you're back to the same issue with the use of absolute time -- as someone once said "time is what keeps everything from happening all at once" -- you've got a problem that if you want the two days data to appear continuous then there's no place to put labels for the two points that aren't consistent in their position with the other spacing...the above is one way to work around that; label the beginning of second day but not the last point of the first.
There is the double line from the grid that does make a slight demarcation where the day end is; that may be useful in reading the plot; you'll have to choose whether you want to keep the two adjacent points or just eliminate one entirely.
ADDENDUM
If you want to retain the color separation between the two days (probably not a bad idea; will help out some on the separation issue) change the plot statement just slightly...
plot([[A.p1;nan(size(A.p1))] [nan(size(A.p1));B.p2]])
creates two series of full length with alternate halves of NaN to keep their relative places. Alternatively, you can use the x-positions explicitly--
x=[1:length(A.p1)].';
plot(x,A.p1,x+length(A.p1),B.p1)